reboot daily at a random minute in a range

Post new topic   Reply to topic    DD-WRT Forum Index -> Contributions Upload
Author Message
SurprisedItWorks
DD-WRT Guru


Joined: 04 Aug 2018
Posts: 1444
Location: Appalachian mountains, USA

PostPosted: Mon Oct 11, 2021 13:32    Post subject: reboot daily at a random minute in a range Reply with quote
Disable scheduled reboot and put this in Startup, editing the assigned values of variables fr and to as desired:
Code:
( #reboot tomorrow at random minute m with $fr <= m <= $to
  fr=01:11 to=05:26
  ep(){ date -d$1 +%s; } #sec since epoch of time $1 TODAY
  while [[ $(nvram get ntp_done) = 0 ]] ; do sleep 60; done
  frEp=$(ep $fr)
  diffm=$((($(ep $to)-$frEp)/60))
  bootEp=$((frEp+86400+60*($(hexdump -n3 -e '"%u\n"' /dev/urandom)%($diffm+1))))
  echo "sleeping until reboot planned for $(date -d@$bootEp)" \
  | logger -t SleepToBoot
  sleep $(($bootEp-$(date +%s)))
  reboot ) &
If the number of minutes between to and fr is a power of two, as in this example, the distribution of the random boot time is exactly uniform. If it is not a power of two, the distribution is very nearly uniform. The planned reboot time is logged in the syslog. Keep both fr and to on one side of midnight or the other. Don't try to have midnight between them, as that option would have required me to think harder than I did.

Edit: The original version also used a little function of mine called DoneFlag that you don't have here and don't need. All cleaned up now.

_________________
2x Netgear XR500 and 3x Linksys WRT1900ACSv2 on 53544: VLANs, VAPs, NAS, station mode, OpenVPN client (AirVPN), wireguard server (AirVPN port forward) and clients (AzireVPN, AirVPN, private), 3 DNSCrypt providers via VPN.
Sponsor
jetblast2k
DD-WRT Novice


Joined: 30 Jan 2022
Posts: 1

PostPosted: Sun Jan 30, 2022 3:15    Post subject: Reply with quote
Thanks!
yoyoma2
DD-WRT User


Joined: 24 Sep 2016
Posts: 371

PostPosted: Mon Jan 31, 2022 4:59    Post subject: Reply with quote
Pardon my ignorance but what's bad about dd-wrt's scheduled reboots using a fixed time? Security risk?
SurprisedItWorks
DD-WRT Guru


Joined: 04 Aug 2018
Posts: 1444
Location: Appalachian mountains, USA

PostPosted: Mon Jan 31, 2022 16:47    Post subject: Reply with quote
yoyoma2 wrote:
Pardon my ignorance but what's bad about dd-wrt's scheduled reboots using a fixed time? Security risk?

I'm not sure there's anything bad about a fixed time, actually. But my router downloads a dozen or so files, mostly for ad/malware blocking, through the vpn client just after everything comes up, and it just feels better not being the guy who downloads every day at 3:30am or whatever.

So there's nothing high priority at all about adding this feature. It's a minor thing that provided an interesting scripting exercise to help keep my aging brain functioning!

_________________
2x Netgear XR500 and 3x Linksys WRT1900ACSv2 on 53544: VLANs, VAPs, NAS, station mode, OpenVPN client (AirVPN), wireguard server (AirVPN port forward) and clients (AzireVPN, AirVPN, private), 3 DNSCrypt providers via VPN.
yoyoma2
DD-WRT User


Joined: 24 Sep 2016
Posts: 371

PostPosted: Mon Jan 31, 2022 17:35    Post subject: Reply with quote
That makes sense and resolves my FOMO. Thanks for the explanation and yes it's a cool script. Very Happy
the-joker
DD-WRT Developer/Maintainer


Joined: 31 Jul 2021
Posts: 2146
Location: All over YOUR webs

PostPosted: Mon May 09, 2022 17:18    Post subject: Reply with quote
SurprisedItWorks wrote:
It's a minor thing that provided an interesting scripting exercise to help keep my aging brain functioning!

Wiser words haven't been spoken, or they have and no one listened.

Great contribution with A+ for the brain thing.

IDK if there is any advantage in saving this script as a Firewall script rather than startup script, but Firewall scripts will run even after configuration changes and most especially after clicking apply, since some services are restarted on clicking apply...

Alternative designs for lesser flash endowed routers would likely be running this from a usb and calling it from startup or firewall or whatever.

_________________
Saving your retinas from the burn!🔥
DD-WRT Inspired themes for routers
DD-WRT Inspired themes for the phpBB Forum
DD-WRT Inspired themes for the SVN Trac & FTP site
Join in for a chat @ #style_it_themes_public:matrix.org or #style_it_themes:discord

DD-WRT UI Themes Bug Reporting and Discussion thread

Router: ANus RT-AC68U E1 (recognized as C1)
SurprisedItWorks
DD-WRT Guru


Joined: 04 Aug 2018
Posts: 1444
Location: Appalachian mountains, USA

PostPosted: Wed May 11, 2022 16:06    Post subject: Reply with quote
Thanks, the-joker. Good to encounter a kindred spirit re brain exercise!

Anyway, if the script were put in the firewall script, it indeed might be rerun on certain Apply steps, etc. In fact I found, when I last tested it some builds back, that the firewall script runs 4x in a normal boot (with my perverse configuration anyway).

Trouble is, the script as is would not clean up from a previous run before starting the new one, so running it 4x times means 4 running processes, each sleeping but for a different time and each set to reboot when the sleep is done. It would reboot at the first of the four times!

To dodge this, someone more concerned and less lazy than I would need to modify the script to subshell the sleep/reboot part and save its PID for the next run of the script to use to delete the already running sleep/reboot. Ick!

_________________
2x Netgear XR500 and 3x Linksys WRT1900ACSv2 on 53544: VLANs, VAPs, NAS, station mode, OpenVPN client (AirVPN), wireguard server (AirVPN port forward) and clients (AzireVPN, AirVPN, private), 3 DNSCrypt providers via VPN.
the-joker
DD-WRT Developer/Maintainer


Joined: 31 Jul 2021
Posts: 2146
Location: All over YOUR webs

PostPosted: Wed May 11, 2022 16:23    Post subject: Reply with quote
Yes, I though saving as firewall may trigger it more often. Scripts can always get better, like you mentioned. A job for another someone else's brain exercise, maybe Wink.
_________________
Saving your retinas from the burn!🔥
DD-WRT Inspired themes for routers
DD-WRT Inspired themes for the phpBB Forum
DD-WRT Inspired themes for the SVN Trac & FTP site
Join in for a chat @ #style_it_themes_public:matrix.org or #style_it_themes:discord

DD-WRT UI Themes Bug Reporting and Discussion thread

Router: ANus RT-AC68U E1 (recognized as C1)
Display posts from previous:    Page 1 of 1
Post new topic   Reply to topic    DD-WRT Forum Index -> Contributions Upload All times are GMT

Navigation

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum