What's new

Help with rerunning the script.

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

Gill

Occasional Visitor
Hey Guys, I am a bit of a noob when it comes to scripting..and need your help and expertise please..I am running a script 'services-start' and it runs fine but whenever I do config change everything reverts to original. I see Merlin has wrote in wiki regarding this:

"services-start
After all other system services have been started at boot. This is the best place to stop one of these services, and restart it with a different configuration, for example (be aware that any time the service gets manually restarted it will revert back to the original setup however)."

Is there a way I can make sure the script runs or is running. I tried cron job to rerun the script like every 1 minute but its not working. so any help would be really appreciated. All I want is that I don't have to worry if the script is running or not.

cron job:
cru a Wifi_Check "*/1 * * * * /jffs/scripts/services-start.sh/"


Regards.
 
If your objective is to make sure that services-start is running or not,
put a debug statement in the script by logging something into system log
which you can view through Admin UI->System Log

Login through ssh/telnet into your router
cd /jff/scripts

nano services-start

Add the below line to the script
logger "In services-start. Starting Services...."

Typically this is the below line which is also found in services-start custom script
/opt/etc/init.d/rc.unslung start

Add that line too if not present
 
This is my services-start file.

RT-AC66U-3F60:/jffs/scripts# cat services-start
#!/bin/sh
PATH="/sbin:/usr/sbin:/bin:/usr/bin:${PATH}"
robocfg vlan 10 ports "0t 1 2 3 4 8t"
robocfg vlan 30 ports "0t 8t"
robocfg vlan 1 ports "0t 8t"
vconfig add eth0 10
ifconfig vlan10 up
vconfig add eth0 30
ifconfig vlan30 up
brctl delif br0 wl0.1
brctl delif br0 wl1.1
brctl addif br0 vlan10
brctl addbr br1
brctl addif br1 wl0.1
brctl addif br1 wl1.1
brctl addif br1 vlan30
ifconfig br1 up
nvram set lan_ifnames="vlan10 vlan1 eth1 eth2"
nvram set lan_ifname="br0"
nvram set lan1_ifnames="vlan30 wl0.1 wl1.1"
nvram set lan1_ifname="br1"
nvram commit
killall eapd
eapd

Normally when i reload..it works fine but when i do some change in GUI..all the stuff in here reverts to original..like no br1 and no vlan 30.. everyone connecting on guest network goes to br0 and gets vlan10 IP. My objective is to retain the following config..which should be possible by running a cron job but clearly I am doing something wrong and cron job is not running the script every minute like its suppose to.
 
services-start.sh
does the file services.start.sh exist?
Just to make clear you understand this correctly:
If you want to schedule to run services-start.sh with cron then you'd add this into the file /jffs/scripts/services-start (Note: no sh ending):
Code:
cru a Wifi_Check "*/1 * * * * /jffs/scripts/services-start.sh"
to run services-start.sh every minute.
Your services-start.sh has the code in it to do your Wifi_Check.

But I think it is best to give your script a better name like wifi_check.sh and put it into the jffs/scripts/ directory.
The content would be your settings:
Code:
#!/bin/sh
PATH="/sbin:/usr/sbin:/bin:/usr/bin:${PATH}"
robocfg vlan 10 ports "0t 1 2 3 4 8t"
robocfg vlan 30 ports "0t 8t"
robocfg vlan 1 ports "0t 8t"
vconfig add eth0 10
ifconfig vlan10 up
vconfig add eth0 30
ifconfig vlan30 up
brctl delif br0 wl0.1
brctl delif br0 wl1.1
brctl addif br0 vlan10
brctl addbr br1
brctl addif br1 wl0.1
brctl addif br1 wl1.1
brctl addif br1 vlan30
ifconfig br1 up
nvram set lan_ifnames="vlan10 vlan1 eth1 eth2"
nvram set lan_ifname="br0"
nvram set lan1_ifnames="vlan30 wl0.1 wl1.1"
nvram set lan1_ifname="br1"
nvram commit
killall eapd
eapd
Then you would add this to /jffs/scripts/services-start:
Code:
#!/bin/sh
cru a Wifi_Check "*/1 * * * * /jffs/scripts/wifi_check.sh"
Also, they need to have proper permissions, set them with:
Code:
chmod a+rx /jffs/scripts/*
 
Last edited:
Awesome I see it running now..Thanks..Now I will try to tweak it further so it doesnt try to create those Vlans.

Feb 18 00:43:01 crond[266]: crond: USER gill22 pid 682 cmd /jffs/scripts/WifiCheck.sh
Feb 18 00:43:01 kernel: register_vlan_device: ALREADY had VLAN registered
Feb 18 00:43:01 kernel: register_vlan_device: ALREADY had VLAN registered
Feb 18 00:44:01 crond[266]: crond: USER gill22 pid 709 cmd /jffs/scripts/WifiCheck.sh
Feb 18 00:44:01 kernel: register_vlan_device: ALREADY had VLAN registered
Feb 18 00:44:01 kernel: register_vlan_device: ALREADY had VLAN registered
 
Good.
Just to check: Did you put the shebang at the top of the services-start file:
#!/bin/sh
 
Yup..It works fine now.. I put the Original script in services-start file along with a call to WifiCheck1.sh which runs every minute to change the interfaces to Br1, Br2 if they merged to Br0 as services-start revert to original if you do any config change that require services to be started. Now I am wondering If I should put a check in WifiCheck which makes it run only if guest interfaces move to br0..if they haven't then the script exits. is there any way to do this?

services-start
#!/bin/sh
PATH="/sbin:/usr/sbin:/bin:/usr/bin:${PATH}"
robocfg vlan 10 ports "0t 1 2 3 4 8t"
robocfg vlan 20 ports "0t 8t"
robocfg vlan 30 ports "0t 8t"
robocfg vlan 1 ports "0t 8t"
vconfig add eth0 10
ifconfig vlan10 up
vconfig add eth0 20
ifconfig vlan20 up
vconfig add eth0 30
ifconfig vlan30 up
brctl delif br0 wl0.1
brctl delif br0 wl1.1
brctl delif br0 wl0.2
brctl delif br0 wl1.2
brctl addif br0 vlan10
brctl addbr br1
brctl addif br1 wl0.1
brctl addif br1 wl1.1
brctl addif br1 vlan30
ifconfig br1 up
brctl addbr br2
brctl addif br2 wl0.2
brctl addif br2 wl1.2
brctl addif br2 vlan20
ifconfig br2 up
nvram set lan_ifnames="vlan10 vlan1 eth1 eth2"
nvram set lan_ifname="br0"
nvram set lan1_ifnames="vlan30 wl0.1 wl1.1"
nvram set lan1_ifname="br1"
nvram set lan2_ifnames="vlan20 wl0.2 wl1.2"
nvram set lan2_ifname="br2"
nvram commit
killall eapd
eapd
cru a Wifi_Check "*/5 * * * * /jffs/scripts/WifiCheck1.sh"



WifiCheck1.sh <<< This should only run if the interfaces merge into BR0 rather than running every T minutes.
#!/bin/sh
PATH="/sbin:/usr/sbin:/bin:/usr/bin:${PATH}"
brctl delif br0 wl0.1
brctl delif br0 wl1.1
brctl delif br0 wl0.2
brctl delif br0 wl1.2
brctl addif br0 vlan10
brctl addbr br1
brctl addif br1 wl0.1
brctl addif br1 wl1.1
brctl addif br1 vlan30
ifconfig br1 up
brctl addbr br2
brctl addif br2 wl0.2
brctl addif br2 wl1.2
brctl addif br2 vlan20
ifconfig br2 up
nvram set lan_ifnames="vlan10 vlan1 eth1 eth2"
nvram set lan_ifname="br0"
nvram set lan1_ifnames="vlan30 wl0.1 wl1.1"
nvram set lan1_ifname="br1"
nvram set lan2_ifnames="vlan20 wl0.2 wl1.2"
nvram set lan2_ifname="br2"
nvram commit
killall eapd
eapd
 
Now I am wondering If I should put a check in WifiCheck which makes it run only if guest interfaces move to br0..if they haven't then the script exits. is there any way to do this?

Try this at the start of your code..

Code:
# Check if br0 WiFi Guest interfaces exist
if [ "$(brctl show br0 | grep -c wl )" = "0" ]; then
   exit 0
fi

<your code to (re)create the bridges>

However I strongly suggest you follow @thelonelycoder's advice and create a separate script such as Create_VLAN_bridges.sh then you only need to maintain one script that can be called from both services-start and a cron job.
 
Try this at the start of your code..

Code:
# Check if br0 WiFi Guest interfaces exist
if [ "$(brctl show br0 | grep -c wl )" = "0" ]; then
   exit 0
fi

<your code to (re)create the bridges>

However I strongly suggest you follow @thelonelycoder's advice and create a separate script such as Create_VLAN_bridges.sh then you only need to maintain one script that can be called from both services-start and a cron job.

Yeah I am following his advise. Only issue is when I put the whole script in WifiCheck.sh, the cron job runs the whole script like creating vlans n bridges every few minutes..and gives errors like vlan already registered..so I added full script to services-start and also added cron job to just call the part that needs to be fixed from WifiCheck1.

I think brctl show br0 | grep -c wl lists all 3 bridges and not just br0..I am understanding right..when the script is working fine..the output of this command should be 0 and it should exit..but I see the output is 4 because its counting the other 2 bridges in as well..

RT-AC66U-3F60:/tmp/home/root# brctl show br2 | grep -c wl
4


RT-AC66U-3F60:/tmp/home/root# brctl show br0
bridge name bridge id STP enabled interfaces
br0 8000.74d0xxxxxx60 no vlan1
eth1
eth2
vlan10
br1 8000.74d0xxxxxx60 no wl0.1
wl1.1
vlan30
br2 8000.74d0xxxxxx60 no wl0.2
wl1.2
vlan20

Regards
 
Yeah I am following his advise. Only issue is when I put the whole script in WifiCheck.sh, the cron job runs the whole script like creating vlans n bridges every few minutes..and gives errors like vlan already registered..so I added full script to services-start and also added cron job to just call the part that needs to be fixed from WifiCheck1.

I think brctl show br0 | grep -c wl lists all 3 bridges and not just br0..I am understanding right..when the script is working fine..the output of this command should be 0 and it should exit..but I see the output is 4 because its counting the other 2 bridges in as well..

RT-AC66U-3F60:/tmp/home/root# brctl show br2 | grep -c wl
4


RT-AC66U-3F60:/tmp/home/root# brctl show br0
bridge name bridge id STP enabled interfaces
br0 8000.74d0xxxxxx60 no vlan1
eth1
eth2
vlan10
br1 8000.74d0xxxxxx60 no wl0.1
wl1.1
vlan30
br2 8000.74d0xxxxxx60 no wl0.2
wl1.2
vlan20

Regards

Whoops, :oops: so I guess you'll have to grep/awk/sed out the extraneous lines then! :p...or does the nvram variable get reset?...if so then check the nvram variable rather than the 'brctl' output
 
Whoops, :oops: so I guess you'll have to grep/awk/sed out the extraneous lines then! :p...or does the nvram variable get reset?...if so then check the nvram variable rather than the 'brctl' output
Yeah I believe this should work fine:
When script is already working :
RT-AC66U-3F60:/tmp/home/root# nvram show | grep lan_ifnames | grep -c wl
size: 48513 bytes (17023 left)
0
RT-AC66U-3F60:/tmp/home/root# nvram show | grep lan_ifname
lan_ifnames=vlan10 vlan1 eth1 eth2


When script needs to run:
RT-AC66U-3F60:/tmp/home/root# nvram show | grep lan_ifnames | grep -c wl
size: 48543 bytes (16993 left)
1
RT-AC66U-3F60:/tmp/home/root# nvram show | grep lan_ifname
lan_ifnames=vlan1 eth1 eth2 wl0.1 wl0.2 wl1.1 wl1.2
 
Thanks a lot guys..Everything is working as it should now. I also added the logger "In services-start. Starting Services...." to the end of WifiCheck1.sh along with the code..Everytime the whole WifiCheck1.sh executes now..it prints the line "In services-start. Starting Services...." in system log..which indicates the whole script ran..otherwise just the first few lines of if fi code runs and exits which is listed as cron jon. Here are my scripts as of now:

System Log:
Feb 18 19:46:01 crond[278]: crond: USER gill22 pid 7236 cmd /jffs/scripts/WifiCheck1.sh
Feb 18 19:46:01 kernel: device wl0.1 left promiscuous mode
Feb 18 19:46:01 kernel: br0: port 5(wl0.1) entering disabled state
Feb 18 19:46:01 kernel: device wl1.1 left promiscuous mode
Feb 18 19:46:01 kernel: br0: port 7(wl1.1) entering disabled state
Feb 18 19:46:01 kernel: device wl0.2 left promiscuous mode
Feb 18 19:46:01 kernel: br0: port 6(wl0.2) entering disabled state
Feb 18 19:46:01 kernel: device wl1.2 left promiscuous mode
Feb 18 19:46:01 kernel: br0: port 8(wl1.2) entering disabled state
Feb 18 19:46:01 kernel: device wl0.1 entered promiscuous mode
Feb 18 19:46:01 kernel: br1: port 1(wl0.1) entering learning state
Feb 18 19:46:01 kernel: device wl1.1 entered promiscuous mode
Feb 18 19:46:01 kernel: br1: port 2(wl1.1) entering learning state
Feb 18 19:46:01 kernel: device wl0.2 entered promiscuous mode
Feb 18 19:46:01 kernel: br2: port 1(wl0.2) entering learning state
Feb 18 19:46:01 kernel: device wl1.2 entered promiscuous mode
Feb 18 19:46:01 kernel: br2: port 2(wl1.2) entering learning state
Feb 18 19:46:04 gill22: In services-start. Starting Services....
Feb 18 19:46:16 kernel: br1: topology change detected, propagating
Feb 18 19:46:16 kernel: br1: port 1(wl0.1) entering forwarding state
Feb 18 19:46:16 kernel: br1: topology change detected, propagating
Feb 18 19:46:16 kernel: br1: port 2(wl1.1) entering forwarding state
Feb 18 19:46:16 kernel: br2: topology change detected, propagating
Feb 18 19:46:16 kernel: br2: port 1(wl0.2) entering forwarding state
Feb 18 19:46:16 kernel: br2: topology change detected, propagating
Feb 18 19:46:16 kernel: br2: port 2(wl1.2) entering forwarding state
Feb 18 19:48:01 crond[278]: crond: USER gill22 pid 7266 cmd /jffs/scripts/WifiCheck1.sh
Feb 18 19:50:01 crond[278]: crond: USER gill22 pid 7276 cmd /jffs/scripts/WifiCheck1.sh
Feb 18 19:52:01 crond[278]: crond: USER gill22 pid 7283 cmd /jffs/scripts/WifiCheck1.sh



services-start
#!/bin/sh
PATH="/sbin:/usr/sbin:/bin:/usr/bin:${PATH}"
robocfg vlan 10 ports "0t 1 2 3 4 8t"
robocfg vlan 20 ports "0t 8t"
robocfg vlan 30 ports "0t 8t"
robocfg vlan 1 ports "0t 8t"
vconfig add eth0 10
ifconfig vlan10 up
vconfig add eth0 20
ifconfig vlan20 up
vconfig add eth0 30
ifconfig vlan30 up
brctl delif br0 wl0.1
brctl delif br0 wl1.1
brctl delif br0 wl0.2
brctl delif br0 wl1.2
brctl addif br0 vlan10
brctl addbr br1
brctl addif br1 wl0.1
brctl addif br1 wl1.1
brctl addif br1 vlan30
ifconfig br1 up
brctl addbr br2
brctl addif br2 wl0.2
brctl addif br2 wl1.2
brctl addif br2 vlan20
ifconfig br2 up
nvram set lan_ifnames="vlan10 vlan1 eth1 eth2"
nvram set lan_ifname="br0"
nvram set lan1_ifnames="vlan30 wl0.1 wl1.1"
nvram set lan1_ifname="br1"
nvram set lan2_ifnames="vlan20 wl0.2 wl1.2"
nvram set lan2_ifname="br2"
nvram commit
killall eapd
eapd
cru a Wifi_Check "*/2 * * * * /jffs/scripts/WifiCheck1.sh"


WifiCheck1.sh
#!/bin/sh
PATH="/sbin:/usr/sbin:/bin:/usr/bin:${PATH}"
# Check if br0 WiFi Guest interfaces exist
if [ "$(nvram show | grep lan_ifnames | grep -c wl)" = "0" ]; then
exit 0
fi
brctl delif br0 wl0.1
brctl delif br0 wl1.1
brctl delif br0 wl0.2
brctl delif br0 wl1.2
brctl addif br0 vlan10
brctl addif br1 wl0.1
brctl addif br1 wl1.1
brctl addif br1 vlan30
brctl addif br2 wl0.2
brctl addif br2 wl1.2
brctl addif br2 vlan20
nvram set lan_ifnames="vlan10 vlan1 eth1 eth2"
nvram set lan_ifname="br0"
nvram commit
killall eapd
eapd
logger "In services-start. Starting Services...."
 

Similar threads

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top