Crontab is a utility for running scheduled tasks at regular intervals on Unix-Linux systems. It allows us to schedule multiple cron jobs to run at once. We can schedule any shell command or script to can be executed on the terminal. Every scheduled job in crontab is separated by a new line. We can also define multiple commands or scripts in a single cron job to run one by one.

How to Sepreate Two Commands in Linux

You can separate two or more commands by using semicolons (;), logical AND (&&), or logical OR (||) operators. Which of these operators we use, is totally depends on the requirements. Here is the basic understanding of using these operators.

  1. Semicolon (;): is used to separate multiple commands. This executes all the commands without checking the exit status of previous commands.
    command_1;  command_2;  command_n
    
  2. Logical AND (&&): is used to separate commands when 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 (||): is used to separate commands when 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
    

How to Schedule a Cron Job

First, switch to the user from which you want to run a cron job. Then open the crontab editor by running the following command.

crontab -e 

Then you add a cron job entry to the file like below:

How to Run Multiple Commands in One Cron Job commands cron cronjobs crontab Linux Tutorials
Crontab commands separated with logical AND

Running Multiple Commands in Single Cron Job

Let’s discuss the real-life examples of running multiple commands with crontab with different-2 separates.

  1. Using Semicolon (;)

    We can separate two or more commands with semicolons, that don’t require checking the exit status of the previous command. For example, you need to change the permission of all files to 777 but need to set 777 for the logs directory.

    0 2 * * *   chmod R 755 /var/www/myapp; chmod R 777 /var/www/myapp/logs

    How to Run Multiple Commands in One Cron Job commands cron cronjobs crontab Linux Tutorials
    Crontab commands separated with semicolon (;)

  2. Using Logical AND (&&)

    Use this operator, where you want to run the next command only if the previous is executed successfully (exit status 0). For example, you want to run the backup.sh after successfully changing to /backup directory.

    0 2 * * *   cd /backup && bash backup.sh

    How to Run Multiple Commands in One Cron Job commands cron cronjobs crontab Linux Tutorials
    Crontab commands separated with logical AND

  3. Using Logical OR (||)

    Use the logical OR (||) operator, when you want to run the next command only if the previous is failed (exit status non-0). For example, you want to show a message or send an email if the backup file not found.

    0 2 * * *  [ f /backup/mydb`date %F`.sql ] || echo “Today’s backup file not found”

    How to Run Multiple Commands in One Cron Job commands cron cronjobs crontab Linux Tutorials
    Crontab commands seperated with logical AND

Conclusion

In this blog post, you have learned about running multiple commands in a single cron job entry. Also discussed various options to separate commands. The article provides you the basic details about each separator that is used to separate commands.