> Am 16.05.2025 um 17:33 schrieb Jan Stary <h...@stare.cz>: > >>> How exactly are you using RANDOM in a crontab, >> >> * * * * * time $((RANDOM % 60));/home/username/bin/script.sh > > That doesn't make any sense: do you mean sleep(1) instead of time(1)?
Sorry, yes. sleep is correct. > Show the actual lines from the actual crontab -l > >> The (intended) semantics are slightly different from >> ~ * * * * /home/username/bin/script.sh >> The latter calculates the random value once when the crontab is loaded, >> while the former calculates it each time the crontab entry is triggered. > > Why do you need that? a) To get more randomness. b) To get seconds resolution instead of minutes. > >> And the former achieves a resolution of seconds instead of minutes. > > Why do you need that? The original issue involved updates to a DDNS service where the server side had high load around full minutes, full 5 minutes, etc. This was most likely caused by bunching up of requests from cron tasks around those times. Since most hosts use NTP to synchronise their clocks, most of the cron-triggered tasks will run almost exactly on full minutes. So if many hosts do this the server side gets a load peak at that time. The object was to find a way to distribute the requests better. > > Are you prepared for the situation when the script runs on the 59th second > of one minute, and on the 01th second of the next minute? Yes, I’m aware of that issue. Not so much different in concept than a ~/13 * * * * specifier shortening the interval at the end of the hour to less than the regular interval. The solution is to use a more complicated expression that leaves a window of time when the the main script is not triggered. (And yes, I’m aware that jot(1), shuf(1) on Linux, and awk(1) can be used to generate random integers in a range as well. RANDOM just seemed like a good alternative.) > >>> and how exactly does it not work? > >> Given these two crontab lines the second never produces output: >> * * * * * echo "$(date)">>/home/username/test-trigger-a.log >> * * * * * time $((RANDOM % 60));echo >> "$(date)">>/home/username/test-trigger-b.log > > But surely it produces an error message, > as "time NUMBER" is not valid syntax. Sorry, `sleep NUMBER` was meant and it is correct syntax. (Apparently I was still asleep when typing ;-) > >> But you are correct: It is not RANDOM that is the problem, >> but apparently the $((...)) calculation does not work. E.g. this works: >> * * * * * echo $RANDOM>>/home/username/test-trigger-c.log >> This doesn’t: >> * * * * * echo $((RANDOM % 60))>>/home/username/test-trigger-d.log > > Read about the ‘%’ char in crontab(5). That was very helpful advice! Thank you! Escaping the % solved the problem. I somehow missed that in crontab(5). Mike