Our task today is to show you how to run multiple commands in one cron job. In this guide we showed you how to run a Python script using cron job. The question is, how would you run several scripts without creating seperate cron jobs?
Crontab Utility
Crontab is a utility on Unix, Linux and Mac systems used for running scheduled tasks at regular intervals on those systems. With crontab you can schedule multiple cron jobs to run at once. This can be done on any shell command or script that you would normally execute on the terminal.
Scheduled jobs in crontab are created on a new line, however, this does not always have to be the can. If we have several scheduled tasks that are supposed to run at the same time and interval, we can also define all those commands or scripts in a single cron job.
A Little Background
Before we see how to execute multiple commands in a single cron job command, we need to understand how those commands or scripts are run by the shell terminal.
In any Linux-Unix you can run one or more commands in a single line. When running two or more commands you will generally separate them by using either semicolons (;), logical AND (&&), or logical OR (||) operators. Each of these separators are used based on the specific requirements as explained below:
1. Semicolon (;) Separator: is used to separate multiple commands. This is guaranteed to execute all the commands without checking the exit status of previous commands.
command_1; command_2; ... command_n
2. Logical AND (&&) operator: is used to separate commands where we want to execute the next command only if the previous command was successfully executed with exit status 0.
command_1 && command_2 && ... command_n
3. Logical OR (||) operator: is used to separate commands where we want to execute the next command only if the previous command failed with a non-0 exit status.
command_1 || command_2 || ... command_n
Execute Multiple Commands in a Single Cron Job
Executed as the user from which you want to run a cron job, open the crontab editor by running the following command.
$ crontab -e
For more details about crontab, we have included a guide to understand how to set up the cron jobs. In our examples below we will use a cron job scheduled to run every 15 minutes.
The Semicolon (;) Option
In this situation it is ideal for running commands that are not related or do not depend on one another. In this example we will run a random script and delete a log file every 15 minutes.
*/15 * * * * python /path/to/app/cronscript.py; rm /path/to/logs/logfile
The Logical AND (&&) Option
In this situation we want to run the next command only if the previous is returns successfully with exit status 0. In this example, you want to extract a file only after successfully downloading the latest copy and delete the downloaded file only after successful extraction.
*/15 * * * * wget https://example.com/downloads/latest.tar.gz && tar -xzvf latest.tar.gz && rm latest.tar.gz
The Logical OR (||) Option
With the logical OR (||) operator, you want to run the next command only if the previous is failed with exit status non-0. In this example, we want to send an email if the log file not found.
*/15 * * * * [ -f /important/log/logfile-`date +%F`.log ] || echo "Warning important message" | mailx -s 'Warning subject' [email protected]
Conclusion
In this article, you have learned how to execute multiple commands in a single cron job command. We also looked at the various options to separate commands and shown example scenarios of how and why you would choose to use either one of them.
Found this article interesting? Follow Brightwhiz on Facebook, Twitter, and YouTube to read and watch more content we post.