Finding a method on how to run multiple commands in the same cron Job on Linux can help users run their scripts simultaneously. Let’s see how it is done!
Cron Job: The Basics
A cron job is a command-line utility in Linux that works like a job scheduler. Linux users use this utility to schedule jobs, commonly known as Cron Jobs. These jobs run at a fixed time interval or periodically. Linux has a Cron daemon responsible for executing the scheduled jobs, commands, or scripts at a specified time or date.
The daemon allows users to perform multiple scripts or commands, in the same manner using the crontab. By using cron jobs, the users can automate maintenance, monitoring, and backups. It is super useful for systems that run 24/7, such as servers. The default crontab file is located at /etc/cron.*/
.
In this article, we will cover what cron jobs are, how to set them up, and run multiple commands in the same cron job on Linux using crontab.
Syntax
The syntax for the crontab expression is:
m h dom m d <Path to the command/script/function>
Where each letter represents:
- m: Minute (0 to 59)
- h: Hour (0 to 23)
- dom: Day of Month (0 to 31)
- mon: Month (0 to 12)
Cron executes the scheduled command when any of the constraints match the current time. For example:
0 0 5-20/5 Feb 2 /path/to/command
The cron job will run once daily every five days, from the 5th to the 20th of February, including all Tuesdays of February. Hence it is crucial to note that if both days of the month and day of the week have specific values, it will create an OR condition.
Run Multiple Commands in the Same Cron Job on Linux
To run multiple commands in Cron, we use Crontab. In this section, we will create three BASH scripts and then schedule them to run in the same job using the crontab. So let’s get started!
Create Sample Scripts
For this tutorial, we have created three scripts: the first script creates a directory, the second script creates a file, and the last script renames the file if it exists. To make these scripts, first, open the Terminal using “Ctrl + Alt + T” and then access your favorite text editor as shown below:
nano script1.sh
vi script1.sh
Copy and paste the code given below, and make sure to give a unique name to each script when saving the file. Lastly, exit the exit editor by pressing “Ctrl + X” (when using nano editor) or “:wq!” (when using vim editor).
Specifically, type:
#script1.sh
#! /bin/bash
echo “Enter directory name”
read dirName
mkdir $dirName
Output:
Next, type the code given below:
#script2.sh
#! /bin/bash
cd $1
echo “Enter file name with extension”
read fileName
touch $fileName
Followed up with:
#script3.sh
#! /bin/bash
cd $1
echo “Enter file name with extension to rename”
read fileName
echo `mv -v $fileName sidrah.txt`
After that, configure the execute permission for all the scripts. otherwise, the cron won’t be able to run these scripts. For this, execute the command given below:
chmod +x script1.sh
chmod +x script2.sh
chmod +x script3.sh
You should get a similar output:
Create Cron Jobs
After that, open the crontab by typing the command given below:
crontab -e
The crontab file will open using the default text editor. To explicitly change the desired editor for editing the crontab file, execute the following command:
export VISUAL=nano; crontab -e
Add the names of the three scripts that you have created at the bottom of the file:
0 6 /home/sidrah/script1.sh
10 6 /home/sidrah/script2.sh
12 6 /home/sidrah/script3.sh
Output:
The above command schedules the scripts to run in sequence at 6:00, 6:10, and 6:12. Make sure to enter a new blank line after every cron job. Otherwise, the last cron job will never run.
The issue with this syntax is that it stresses the CPU and overuses the bandwidth. To overcome this problem, you can either use the “&&” sign or the semicolon “;”. The difference between the two is that && executes the second file only if the first command is successful.
On the other hand, the semicolon “;” executes all the jobs independently regardless of whether the previous task was successful or not.
Other special characters that you can use to schedule the cron jobs are:
- || […] command1 || command2.
- ( ) […] (command1 & command2)
See Multiple Scheduled Commands on Cron Job in Linux
To review all the scheduled cron jobs on Linux, view the cron table. For instance, type:
nano /var/spool/cron/crontabs
Alternatively, you can also execute the following command for the same output:
crontab -l
You should get a similar output:
Remove Crontab from Linux
In case, you no longer need to use crontab—you can easily remove it. For this step, use the remove
command. Specifically, type:
crontab -r
Examples of Multiple Commands in the Same Cron Job In Linux
Crontab expressions can be as simple as the one shown below:
* * * * ? *
Or as complex as:
0 0/5 14,18,3-39,52 ? JAN,MAR,SEP MON-FRI 2002-2011
You can use crontab to schedule cron jobs in several ways. Let’s look at some of the examples.
To fire a cron job daily at noon, type:
0 0 12 * * ? <path to the file>
Alternatively, to fire a job at a specific time every day, write the expression shown below:
0 15 10 * * ? * <path to the file>
To add a job that will be executed in a specific month, on a fixed day, and at a specified time, the expression would look something like this:
0 11 11 11 11 ? <path to the file>
This cron job will execute at 11:11 AM on November 11th.
Redirect Cron Output in Linux
To redirect the output from the cron job to a file, use the redirection operator “>>
”. For example:
* * * * * <path to the file> >> /var/log/cron.log
Alternatively, to email, the output instead of redirecting to the output file, use the MAILTO
variable. Specifically, type:
[email protected],[email protected]
* * * * * <path to the file>
And that’s a wrap! You have mastered how to run multiple user-lever commands in the same cron job on Linux. For more information on Cron, check out its official documentation. Alternatively, you can use the man command to read its manual on Linux. We hope you found this article to be helpful.
Did you learn anything new from this post? Have we missed anything? Let us know in the comments, and we will include it in our next article.
If this guide helped you, please share it.