Jakob Nielsen
Jakob Nielsen

Reputation: 5198

special cronjob scheduling

I have three scripts and want every one of them to run every 3 minutes, but in way that every minute a different script is running.

for example

00:00 script1 is executed
00:01 script2 is executed
00:02 script3 is executed
00:01 script1 is executed

Is there a way to make this work via crontab in Debian?

At the moment I have it like this:

*/3 * * * * php /Scripts/script1.php &> /dev/null
*/3 * * * * php /Scripts/script2.php &> /dev/null
*/3 * * * * php /Scripts/script3.php &> /dev/null

but this would run all the scripts all 3 minutes

Upvotes: 0

Views: 35

Answers (2)

Walter A
Walter A

Reputation: 20002

Call a wrapper script every minute. This wrapper script looks at (minutes % 3) and calls the correct script using the remainder.
Only one line in cron: nice.

EDIT: New thoughts You can skip the wrapper by introducing an ugly crontab line.
I would go for the wrapper (cleaner crontab, place to set and export variables, additional control statements), but I think you should know about the possibilities.
Make the testfiles x0, x1 and x2 in /tmp, chmod +x them, with the content

echo $(date) $0 >> /tmp/x.out

Make a crontab line

* * * * * /tmp/x`echo "$(date '+\%M') \% 3" | bc`

Wait 5 minutes (maybe get coffee black for me?) and look at /tmp/x.out.
Remove the crontab entry and the new /tmp/x* files.

Upvotes: 1

Mike
Mike

Reputation: 2136

There might be fancier ways, but the dead simple way is just to list out the minutes you want them to run on (and the rest would of course be * for hours, days, etc):

0,3,6,9,12,15,18,21,24,27...
1,4,7,10,13,16,19,22,25,28...
2,5,8,11,14,17,20,23,26,29...

Upvotes: 2

Related Questions