rav2040
New Around Here
Until recently I had been using the script from the following wiki page for scheduled LED control on my Asus RT-AC66U:
https://github.com/RMerl/asuswrt-merlin/wiki/Scheduled-LED-control
However, I wanted to be able to preserve the LED state during reboots. As the wiki page says, this can be done with "nvram commit", but warns that doing so might slightly shorten the flash lifespan.
I decided to see if I could write a script that could essentially "preserve" the LED state without using this command, and came up with the following:
It works by retrieving the system time of the router and using it to determine whether the LEDs should be enabled or disabled, based on the values of ON_TIME and OFF_TIME entered at the start of the script (I use 6 am and 10 pm). Since it takes some time for the system time to synchronize with an NTP server after a router reboot, there's an until loop in there that makes sure the time is synchronised before proceeding. If it fails to synchronise after 60 seconds then the LEDs will just default to enabled.
This script also adds the necessary cron jobs, so all that needs to be done is for this script location to be added to the 'services-start' script (I named the script 'ledctl', but if you name it something else you would also need to change the two lines that create the cron jobs).
I'm just a novice at this, so there could be some glaringly awful problems in this script (please feel free to point them out!) that I'm not aware of; but I've tested it on my RT-AC66U with a bunch of different values for ON_TIME and OFF_TIME and it all seems to work as intended.
https://github.com/RMerl/asuswrt-merlin/wiki/Scheduled-LED-control
However, I wanted to be able to preserve the LED state during reboots. As the wiki page says, this can be done with "nvram commit", but warns that doing so might slightly shorten the flash lifespan.
I decided to see if I could write a script that could essentially "preserve" the LED state without using this command, and came up with the following:
Code:
#!/bin/sh
ON_TIME=6 #Time to turn on LEDs (hour value only)
OFF_TIME=22 #Time to turn off LEDs (hour value only)
#Add LED enable/disable cron jobs
cru a EnableLEDs "0 $ON_TIME * * * /jffs/scripts/ledctl"
cru a DisableLEDS "0 $OFF_TIME * * * /jffs/scripts/ledctl"
#Wait until system time has been synchronized
count=0
until [ "$(nvram get ntp_ready)" == "1" -o $count -gt 60 ]; do
sleep 5
count=$(( count + 5 ))
done
#Enable or disable the LEDS according to values of $ON_TIME and $OFF_TIME
#If system time has not been synchronized then LEDs will default to enabled
if [ "$(nvram get ntp_ready)" == "1" ]; then
current_time=$(echo "$(( $(date '+%H') ))" | sed 's/^0*//')
on_time_diff=$(( current_time - ON_TIME ))
if [ $on_time_diff -lt 0 ]; then
on_time_diff=$(( on_time_diff + 24 ))
fi
off_time_diff=$(( current_time - OFF_TIME ))
if [ $off_time_diff -lt 0 ]; then
off_time_diff=$(( off_time_diff + 24 ))
fi
if [ $on_time_diff -lt $off_time_diff ]; then
nvram set led_disable=0
service restart_leds
logger -t ledctl "LEDs enabled"
else
nvram set led_disable=1
service restart_leds
logger -t ledctl "LEDs disabled"
fi
else
nvram set led_disable=0
service restart_leds
logger -st ledctl "System time has not synchronized; LEDs defaulting to enabled"
fi
It works by retrieving the system time of the router and using it to determine whether the LEDs should be enabled or disabled, based on the values of ON_TIME and OFF_TIME entered at the start of the script (I use 6 am and 10 pm). Since it takes some time for the system time to synchronize with an NTP server after a router reboot, there's an until loop in there that makes sure the time is synchronised before proceeding. If it fails to synchronise after 60 seconds then the LEDs will just default to enabled.
This script also adds the necessary cron jobs, so all that needs to be done is for this script location to be added to the 'services-start' script (I named the script 'ledctl', but if you name it something else you would also need to change the two lines that create the cron jobs).
I'm just a novice at this, so there could be some glaringly awful problems in this script (please feel free to point them out!) that I'm not aware of; but I've tested it on my RT-AC66U with a bunch of different values for ON_TIME and OFF_TIME and it all seems to work as intended.