How to Set Up a Cron Job in Linux
You may often need to run specific tasks automatically without any manual intervention if you are a system admin or developer. It is the place where the Cron jobs come into the picture. For example, cron jobs help automate such tasks, specifically those repetitive ones.
The Cron software is a job scheduler in Linux operating systems that allows Linux users to run a specific command or script at a given time and date. Generally, it is used by system administrators to automate backup tasks, directory cleaning, notifications, etc.
The Cron jobs runs in background and continuously checks /etc/crontab, /etc/cron.*/ and /var/spool/cron/ directories for any jobs and execute them at pre-defined interval of time.
This post will show you how to set up a Cron job in Linux.
Table Of Contents
- Install Cron Package
- Cron Basic Syntax
- How to Set Up a Cron Jobs
- Manage Cron Jobs
A server running Linux operating system.
A root password is set up on your server.
Install Cron Package
Before starting, make sure the Cron package is installed in your system. If not installed, you can install it using the following command:
apt-get install cron -y
dnf install crontabs -y
After installing the Cron package, start the Cron service and enable it to start at system reboot:
systemctl start cron systemctl enable cron
systemctl start crond systemctl enable crond
Cron Basic Syntax
The basic syntax of Cron is shown below:
A B C D E USERNAME COMMAND
A brief explanation of each syntax is shown below:
A - Minutes range from 0 to 59
B - Hours range from 0 to 23
C - Days range from 0 to 31
D - Months range from 0 to 12
E - Days of the week range from 0 to 7
USERNAME - User that runs the Cron job
COMMAND - Command that you want to execute
How to Set Up a Cron Jobs
This section will show you how to set a cron job with examples.
You can create a Cron job by editing /etc/crontab file. Run the following command to edit the crontab file:
It will open a nano or vim editor in your terminal, and you can add your Cron jobs here.
To set up a cron that runs every 5 minutes, add the following line:
*/5 * * * * root /path/of/command
To set up a cron that runs at 6 p.m. every day, add the following line:
0 18 * * * root /path/of/command
To set up a cron that runs at the 15th minute of every 2nd and 9th hour, add the following line:
15 2,9 * * * root /path/of/command
To set up a cron that runs every hour, add the following line:
0 * * * * root /path/of/command
Set up a cron that runs the command every Tuesday, Wednesday, and Thursday at 4:00 AM.
0 4 * * 2-4 root /path/of/command
To set up a cron that runs every Monday at 1 a.m., add the following line:
0 1 * * */MON root /path/of/command
To set up a cron that runs on the first day of every month and Sundays at midnight, add the following line:
0 0 1 * 0 root /path/of/command
You can also use the following strings in the crontab file to set up your jobs quickly.
@hourly: Run once every hour.
@midnight: Run once every day.
@daily: same as midnight.
@weekly: Run once every week.
@monthly: Run once every month.
@yearly: Run once every year.
@reboot: Run once at every startup
Manage Cron Jobs
To list all Cron jobs, run the following command:
To list other user's Cron jobs, run the following command:
crontab -u username -l
To delete all Cron jobs, run the following command:
To delete a Cron job for a specific user, run the following command:
crontab -r -u username
To edit the specific user's Cron jobs, run the following command:
crontab -u username -e
The above guide taught you how to set up a Cron job on Linux. Cron is a simple and powerful utility that helps you to reduce the burden of many tasks associated with system administration. You can now automate many tasks with Cron.
How do I start and stop a cron job in Linux?
By deleting its entry from the crontab file, you can end a single cron task. To accomplish this, enter the crontab -e command and remove the line corresponding to the particular task. The cron job can also be terminated by commenting it in the crontab file.
What is * In cron job?
In Cron, the wildcard character * is used to specify any minute, hour, day, weekday, or month for a task's execution.
Thank you for helping us improve!