Joined: 04 Aug 2018 Posts: 1328 Location: Appalachian mountains, USA
Posted: Mon Oct 11, 2021 13:32 Post subject: reboot daily at a random minute in a range
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. _________________ Six Linksys WRT1900ACSv2 routers on 48141: VLANs, VAPs, NAS, client mode, OpenVPN client (AirVPN), DDNS, wireguard servers and clients (AzireVPN), three DNSCrypt DNS providers (incl Quad9) via VPN clients.
Joined: 04 Aug 2018 Posts: 1328 Location: Appalachian mountains, USA
Posted: Mon Jan 31, 2022 16:47 Post subject:
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! _________________ Six Linksys WRT1900ACSv2 routers on 48141: VLANs, VAPs, NAS, client mode, OpenVPN client (AirVPN), DDNS, wireguard servers and clients (AzireVPN), three DNSCrypt DNS providers (incl Quad9) via VPN clients.
Joined: 31 Jul 2021 Posts: 744 Location: All over YOUR webs
Posted: Mon May 09, 2022 17:18 Post subject:
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...
Joined: 04 Aug 2018 Posts: 1328 Location: Appalachian mountains, USA
Posted: Wed May 11, 2022 16:06 Post subject:
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! _________________ Six Linksys WRT1900ACSv2 routers on 48141: VLANs, VAPs, NAS, client mode, OpenVPN client (AirVPN), DDNS, wireguard servers and clients (AzireVPN), three DNSCrypt DNS providers (incl Quad9) via VPN clients.