Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Say I have several cron scripts that need to run every 15 minutes. I could set them to run: */15 * * * * , but then they all run at the same time. It seems silly for the server to sit idle for several minutes and then suddenly try to execute a dozen scripts all at the same time.

Is there a way I can have one script run at minute 1, 16, 31, 46 and another at 2, 17, 32, 47?

In other words, I want each script to run every 15 minutes, but I don't care that they run specifically on the quarter hour marks.

share|improve this question
add comment

6 Answers

up vote 7 down vote accepted

You're making this harder than it needs to be. Put them all on the same line, separated by semicolons:

*/15 * * * * command1 ; command 2 ; command 3

It will run command1, wait for it to finish, then run command2, wait for it to finish, and so on.

share|improve this answer
add comment

Many distributions have /etc/cron.d/cronhourly, for which all scripts within get run hourly. You can even specify the order by starting them with sequential numbers, lik 01scriptA 02scriptB - it should be trivial with the cron knowledge you already have to manufacture a "cronhourlybyfour" as we call it on Smoothwall's linux distro base :)

Word of warning: this uses run-parts as previously suggested, and run-parts does NOT like scripts with a . in the name, so don't call it "deletehomefolders.sh" call it "01deletehomefolders" and make sure you start with the right #! line for whatever you intend to interpret your script.

share|improve this answer
add comment

You could put all of the scripts into a directory, say /etc/cron.15m, and then have cron run

*/15 * * * * run-parts /etc/cron.15m

That's assuming you have the run-parts command. It's present on all Debian-based systems at least. It runs all of the executable programs in the named directory, one at a time in list order.

A disadvantage of this method is that if one of the scripts hangs, all the rest will wait and not be executed. If the run time of all of them is more than 15 minutes, then the job will start running again and you could get a lot of processes piling up.

share|improve this answer
    
run-parts is present in Fedora / Red Hat systems too. –  mattdm Nov 11 '11 at 3:26
add comment

The most straightforward way to do this is just to manually set up the commands to run when you want them to:

0,15,30,45 * * * * command0
1,16,31,46 * * * * command1
2,17,32,47 * * * * command2
...
14,29,44,59 * * * * command14

Or you can write a script to automatically generate the appropriate crontab entries (which avoid typos).

Some versions of cron (probably including the one you're using) accept an extended syntax:

0-59/15 * * * * command0
1-59/15 * * * * command1
1-59/15 * * * * command2
...
14-59/15 * * * * command14
share|improve this answer
add comment

Cron isn't really good at what you're trying to do. Have you considered writing a script that acts as a daemon that basically sleeps 15 minutes, executes the command, then loops?

share|improve this answer
1  
There are a couple of potential problems with that. One is that if you do sleep 300 between each execution of the command, the command's own execution will cause the time to drift; it might run every 15:10 rather than every 15:00. And if the background process dies or the system reboots, unlike crond, it won't automatically restart. There are ways to "daemonize" a background process, but then you're pretty much re-implementing crond. –  Keith Thompson Nov 2 '11 at 19:38
    
Yeah, I agree it's not a perfect solution (though you could always background the task with &), but there you go. –  Matt Simmons Nov 2 '11 at 21:05
1  
Instead, just have cron run a single script that executes all your commands in turn. In fact, it's such a common need that there's a standard script called run-parts that ships with most cron installations to do specifically that. See @AndrewSchulman's answer. –  tylerl Nov 2 '11 at 22:51
add comment

If you make your cron job look like this: 6-59/15 * * * * then it will run at 6, 21, 36 and 51 minutes past the hour.

This may not work with all versions of cron.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.