Linux crontab command is used to schedule cron jobs. A cron job is a handy automation tool that enables Linux users to execute automated tasks at specific periods or intervals.
This is a friendly way of scheduling tasks which would otherwise be tedious if managed manually. Cron jobs are usually used by system administrators to automate tasks such as server backups, restarts, sending of notifications, and clean-ups to mention just but a few.
A cron job runs silently in the background, checking the
/etc/crontab file and
/etc/cron.*/ directories. In this article, you will learn how to create and manage cron jobs in your Linux system.
Table of Contents
Linux Cron Job Structure
The anatomy of a cron job is as shown below.
* * * * * COMMAND arg1 arg2
* * * * * /path/to/script arg1 arg2
From the left,
The 1st entry represents Minutes [0 - 59] The 2nd entry represents Hours [0 - 24] The 3rd entry represents Days [0 - 31] The 4th entry represents Months [0 - 12] The 5th entry represents Days of the week [0 - 7]
Linux crontab Example
Now that you have the skeleton for a crontab, writing one is quite easy provided you know the command or the path to your script.
In this example, we are going to create a crontab for rebooting a Linux system.
Copy and paste the content below into the bash file.
#!/bin/bash 0 3 * * * /sbin/reboot
Going by the crontab syntax as earlier indicated, the cron job will run daily at 3:00 am and reboot the system.
Save and quit the text editor.
Next, assign the execute permissions as shown.
# chmod +x reboot.sh
To verify file permissions, run:
# ls -l reboot.sh
To initialize the crontab run
# crontab reboot.sh
If you want to view contents of a crontab of a user that is currently logged in user run the command.
With the crontab up and running, the crontab will be executed at the stipulated time, i.e 3:00 am local time.
To remove all cron jobs in the system, use the
Additional examples of running cron jobs at certain time intervals
- To run a cron job every minute, run
* * * * * COMMAND
If the current time is 6:00 am, the cron job will run at 6:01 am, 6:02 and so on.
- To run a cron job after every nth minute, for example after every 10 minutes, run
*/10 * * * * COMMAND
- To run a cron job every nth minute of every hour, for example after every 30 minutes of every hour, execute
30 * * * * COMMAND
- To run a cron job every hour, i.e every 0 minutes, run
0 * * * * COMMAND
- To run a cron job after every n hours, for instance, after every 6 hours, run
0 */6 * * * COMMAND
- To run a cron job at a specific day, say Friday, execute
0 0 * * FRI COMMAND
- If you wish to run a cron job from Monday to Fridayrun
0 0 * * 1-5 COMMAND
This executes the command at 00:00 hours every day from Monday to Friday.
- To execute a cron job everyday, run
0 0 * * * COMMAND
- To execute a cron job every first day of every month
0 0 1 * * COMMAND
- To execute a cron job every first day of the month at a specific time, say 20:30 hours, run
30 20 1 * * COMMAND
- To run a job every quarter of the year, i.e. the 1st day after every 3 months, run
0 0 1 */3 * COMMAND
- To run a cron job every year, run
0 0 1 1 * COMMAND
This cron job will be executed every Friday at exactly 00:00 midnight.
Scheduling cron jobs using Crontab Guru
Crontab syntaxes can be quite confusing and difficult to master. But don’t worry. Crontab Guru is a very friendly online tool that helps you seamlessly define your time intervals without much hassle. It’s a web interface that has crontab’s time intervals well labeled and all that is required is for you to type in the figures into the text field as the tool interprets the crontab. In the example below, the tool generated the crontab for automating a cron job every 6:00 am from Monday to Saturday.