What's new
  • 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!

Alternative scheduled LED control script

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:

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.
 
Thanks a lot for that script! Was going to write something like that by myself, but now I don't have to :P :D
 
What happens if you run in the background and wait indefinitely for the system time to be synchronized (no timeout)?
 
Thanks a lot for that script! Was going to write something like that by myself, but now I don't have to :p :D

You're welcome!

What happens if you run in the background and wait indefinitely for the system time to be synchronized (no timeout)?

To be honest, I hadn't really considered that! I guess my line of thinking was that 60 seconds is plenty of time (mine seems to take about 15-20 seconds), so if it hadn't synchronised by then, then it probably wasn't going to.
 

Latest threads

Support SNBForums w/ Amazon

If you'd like to support SNBForums, just use this link and buy anything on Amazon. Thanks!

Sign Up For SNBForums Daily Digest

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