The “crontab” is the command line utility used to schedule the task at the defined time interval. The crontab allows users to run the scheduled tasks or cron jobs individually or combined. The purpose of running multiple cron jobs is to it is recommended to execute multiple commands or schedule shell scripts in the same cron job at the same time.
The purpose of this guide is to explain the possible ways to run multiple commands in the same cron job. The outline of this guide is mentioned here:
Let’s start with the first method!
Method 1: Using the Logical AND(&&) Operator
The logical AND “&&” operator helps to run multiple commands in the same cron job for the specified time. Basically, it executes the next command after the successful execution of the previous command. The working of the “&&” operator for running multiple commands relies on its basic syntax
$ * * * * * /path/to/command_1 && /path/to/command_2 && ...../path/to/command_N
First the crontab file “/tmp/crontab.08jLm/crontab *” file is created using the “crontab -e” command. In our case, we have joined two commands, “ls” and “chmod”, to schedule the same cron job after every five minutes:
$ crontab -e
The “chmod” command will change the “test.sh” script permissions if the “ls” command list downs the “test” directory content.
Method 2: Using the Semi-Colon(;)
Here is another method to run the multiple commands in the same cron job using the “;(Semi-Colon)”. The “Semi-colon” does not depend on the previous command’s exit status. It executes each command independently defined in the same cron job.
The generalized syntax to run multiple commands in the same cron job using the “;” is written below:
$ * * * * * /path/to/command_1 ; /path/to/command_2 ; ..... /path/to/command_N
In this example, the crontab file holds the three commands joined with “;(Semi-Colon)” in the same cron job that will execute simultaneously for every 10 minutes:
*/10 * * * * rm File1.txt ; cat>NewFile.txt ; ls -l NewFile.txt
Each command will execute independently every 10 minutes.
Alternative Method: Using the Logical OR(||) Operator
The user can use the OR “||” logical operator. But it only runs the next command when the execution of the first command fails. If the first command runs successfully, it does not execute the further commands connected with the “||” operator in the same cron job.
Multiple running commands in the same cron job are not recommended.
Its basic syntax is as follows:
$ * * * * * /path/to/command_1 || /path/to/command_2 ||...../path/to/command_N
It is all about running multiple commands in the same cron job.
That’s how you can run multiple commands in the same cron job.
In Linux, to run multiple commands in the same cron job, use the logical AND “&&” operator and the “;(Semi-Colon)” symbol. The “&&” operator runs the next command when the first command executes successfully. However, the “;” colon runs each command independently. This guide has illustrated all possible ways to run multiple commands in the same cron job.