2

So I've got the following crontab rule (they are called rules, right?) but after a.out is finished, b.out doesn't start.

00 15 * * * /home/jesse/test/a.out && /home/jesse/test/b.out

Both are some software I've created myself in c++. a.out takes about 1.5 hours to complete, b.out less than a minute.

If I swap them over, first b, than a, they both run. I've checked if perhaps a.out returns false or 0 on completion but both a/b return 1. When I run this same command directly in the terminal, both run just fine. This led me to believe this is a crontab thing.

What is going on here? Is my rule incorrect or is the running duration too much?

2
  • 5
    Your C++ programs should return 0 to indicate success and non-zero to indicate failure Nov 7, 2019 at 13:40
  • @steeldriver Thanks, good to know.
    – JesseB1234
    Nov 7, 2019 at 13:58

2 Answers 2

6

Cron should do this with no problem. However, the && means "do the second command only if the first exited successfully". So if the second is being ignored, then most likely the first one failed or, in any case, exited with an exit status != 0.

So, either fix whatever is causing the first command to fail or change the crontab so it runs one command after the other irrespective of whether it worked or not:

00 15 * * * /home/jesse/test/a.out; /home/jesse/test/b.out
5
  • The && is what I wanted, run only b if a succeed. a.out returns '1' as in true after running so it is indeed my own program not returning 0 for succes (seems a bit counter-intuitive, right? 0 for succes?)
    – JesseB1234
    Nov 7, 2019 at 14:18
  • 1
    @JesseB1234 yes and no. I mean, yes, it is counterintuitive if you're coming from a C background. However, the shell always takes 0 as success, you can think of it as written to enable this logic: if($?){ echo "exit status is defined, so something went wrong} where $? is the special variable that holds the exit status of the last command run. So don't think of it as True vs False (1 vs 0) but as "did something go wrong?". Remember that exit codes can take arbitrary values, -1, 1, 128, whatever, and each can correspond to a different failure. Only 0 is special and means success.
    – terdon
    Nov 7, 2019 at 14:25
  • 1
    @JesseB1234 by the way, this behavior is defined not only by POSIX ("A value of zero (or EXIT_SUCCESS, which is required to be zero) for the argument status conventionally indicates successful termination.) but also by the C standard. Basically, don't confuse return value (e.g. what a function returns) with exit status.
    – terdon
    Nov 7, 2019 at 14:57
  • @JesseB1234 0 for success is actually very common in C. Many Linux system functions return 0 for success and -1 on error. Many libraries return 0 for success and an error code on error. Success/failure is not the same thing as true/false. Nov 7, 2019 at 22:09
1

If you have several commands to run from only one crontab entry (though I'd suggest running 2 crontab entries), you can call the commands from a shell script and call the script from the crontab (using the full path to your script).

3
  • I'm running two separate commands now, problem is that b is depended on data generated by a, and beforehand it is unknown how long a takes to run. a takes about 90 min, but can be 180 min in the extreme. And I want the results of b.out ASAP so a large safe time between the both is not optimal.
    – JesseB1234
    Nov 7, 2019 at 14:04
  • Running a scripts is a very good idea, didn’t think of that. In the future more commands will be added after b.out (c/d/e etc) so a shell script would be appropriate for this as well.
    – JesseB1234
    Nov 7, 2019 at 14:08
  • 1
    @JesseB1234 If you know ahead of time more commands will be added, then you definitely want to encapsulate that complexity in a script, and have the crontab entry simply invoke that script. That way you can build in all of the if-then-else and other sophisticated logic a complex situation will require (and && can't deliver) Nov 7, 2019 at 23:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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