1

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?

1 Answer 1

2

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.

1
  • maintenance is a good point. It is significantly easier to change as one job, then as 6 or 7 jobs.
    – nelaaro
    Sep 7, 2012 at 13:27

Not the answer you're looking for? Browse other questions tagged .