I'd like to write a cron command so that a job would execute once at some random time during the week - is this possible without having the crontab modify itself? I've considered writing a function that, based on a random number generator, may or may not execute the task - but I'd like the command to definitely execute and there to be no second in the week that was more likely than any other second...
|
feedback
|
|
One quick solution is to write a cron script that is called once a week and delays itself for a random time (not longer than the week) before doing the real job, for example by using the following command (works for bash and zsh)
| |||
|
feedback
|
|
Use a combination of
In | |||
|
feedback
|
|
A quick (and dirty, and BAD, see comments below) solution to that problem would be creating a bash script which would run everyday as a cron and designed as follow: That shell script would have a 1/7 chance to run your command each day and would update the timestamp of a file each time it successfully runs (use 'touch' for that). There must be a better way but I'm too tired to see it ^^ update: As Joe pointed this script has a big flaw in that it can (and will eventually) not run at all some weeks. Thus you should not use it but I'll leave it as a counter-example. | |||||
feedback
|
|
lacqui is correct, but his explanation is a bit short. cron:
/home/myhome/cron/schedulerandomjob.bash:
cron runs once a week, scheduling an 'at' job which randomly occurs during the week. (nb. There's a corner-case here to watch for... it might be possible for the job to occur at the end of the week and the beginning of the week, which might cause two simultaneous executions. If it is important, subtract a few minutes from the modulo to be sure. | |||
|
feedback
|
