reboot daily at a random minute in a range

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


Joined: 04 Aug 2018
Posts: 1262
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.

_________________
Six Linksys WRT1900ACSv2 routers on 46816: VLANs, VAPs, NAS, client mode, OpenVPN client (AirVPN), DDNS, wireguard servers and clients (AzireVPN), three DNSCrypt DNS providers (incl Quad9) via VPN clients.
Sponsor
Display posts from previous:    Page 1 of 1
Post new topic   Reply to topic    DD-WRT Forum 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