Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How would cron handle a job that executes multiple commands.

As an example I have a cron job that looks like this. Which gets very difficult to read.

10 0 * * * firstscript.sh > first.log; secondscript.sh >> second.log; thirdscript.sh >> third.log 2>&1;

All three scripts execute at the same time. In my environment we have 6 commands executed in sequence.

Why not write that same jobs three separate jobs.

10 0 * * * firstscript.sh > first.log;  
10 0 * * * secondscript.sh >> second.log; 
10 0 * * * thirdscript.sh >> third.log 2>&1;

I inherited these jobs and am trying to get a better understand of why do it as multiple commands in stead of 3 separate jobs.

  • The first reason I can think of that there may be dependencies between the scripts. So they have to run in that specific order. So secondscript.sh depends on firestscript.sh completing.

  • The second reason is that these scripts/commands are expensive to execute and the server runs better when they run sequentially. Potential file locks and multiple commands writing to the same file could also cause issues.

If the first script exits with error, do the commands following it run?
If dependencies are an issues, is there way to split the job into multiple jobs and still satisfy the dependencies?
Does cron have a facility like that?

share|improve this question

closed as not constructive by Bobby, Indrek, Nifle, KronoS, 8088 Sep 10 '12 at 6:00

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer 1

up vote 2 down vote accepted

Concerning your first point, neither or your crontabs ensure proper dependency resolution, because every command is executed regardless of the issue of the previous one. In order to do so you want to use operator && between your commands. This also answers the first of you last 3 questions.

Your second point could be valid, depending on the context. Although I suspect that the reason for writing all commands in one line is that it's easier to just add something in the end than to add a new line with a new schedule. One reason could also be maintenance: if you want to change your schedule for all your commands, it's easier to change just one line.

For the second of your last 3 questions, the answer is yes, but it might be complicated. You have to manually identify which task can safely run concurrently with which other, and setup a mechanism to allow your next set of tasks to know whether the first ones have succeeded or not. Cron does not have anything built in to help you there.

share|improve this answer
    
maintenance is a good point. It is significantly easier to change as one job, then as 6 or 7 jobs. –  nelaar Sep 7 '12 at 13:27

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