Crontab file stores the cron jobs in Linux. Cron jobs (cron) runs periodically at fixed time, dates and intervals. Its equivalent in Windows is scheduled task. All repetitive tasks can be scheduled using cron.
Crontab file is the configuration file which contains information about the time and command/scripts to execute.
In this tutorial we learn Linux crontab with examples to schedule jobs.
Each line in crontab file represents a job. It contains five fields followed by a shell command or script to execute.
* (Minute) *(Hour) * (Day of the Month) *(Month of the Year) *(Day of the Week) username <path to command/script to execute>
|*||Minute||This field value is between 0 - 59|
|*||Hour||This field value is between 0 - 23|
|*||Day of the month||This field value is between 1 - 31|
|*||Month of the year||This field value is between 1 - 12. First 3 alphabet of month is also accepted like jan, feb, mar|
|*||Day of the week||This field value is between 0 - 7. Where 0 and 7 is Sunday. 1 is Monday, 2 is Tuesday , like wise|
Define multiple values or ranges
To define multiple values and ranges use comma (,) and hyphen (-). The asterisk (*) matches anything. For example, to show multiple values use 1,2,3 or mon,tue,wed and for ranges can use 1-3 or mon-wed.
How to use crotab
crontab command is used to open and edit/add cron jobs. The cron daemon that run on Linux execute scheduled jobs at time define in the crontab ( cron table). The main system-wide crontab file is /etc/crontab.
Cron is by default installed in most Linux Distributions. Each user in the Linux system can manage their own cron jobs. Each user cron is stored under /var/spool/cron/crontabs/. The cron daemon runs both user and system crontab.
Cron daemon has some default $PATH defined, which you can find in /etc/crontab. If its not define either use absolute path or add to the cron $PATH variable.
Cron mail notification by default is sent to owner of the crontab. To change this, edit
MAILTO environment variable.
Cron logs are stored in /var/log/syslog on Ubuntu/Debian systems and in /var/log/cron on Redhat/Rocky Linux. You can filter the log file using grep command to verify the cron job ran successfully or not.
1. Edit/add Crontab
To edit (existing jobs) or add new cron use crontab -e command. This will open an editor for the current user.
$ crontab -e
To edit another user crontab file, type:
$ crotab -u username -e
Or simply run crontab -e command by logged in as that user.
2. List Cron Jobs
To list all jobs of the current user, type:
$ crontab -l
To list all cron jobs for a specified user, type:
$ crontab -u -l
3. Remove cron jobs
To remove all cron jobs of the current user, execute the following command:
# crontab -r
Note: This will remove all the cron jobs without asking for confirmation.
If you want to delete interactively, you need to use the following command:
# crontab -ir
Crontab Jobs Examples
Let's check some crontab examples for scheduling cron jobs.
1. Run cron jobs every 12 hours
This helps for regular system checks or backups. To schedule a task to run every 12 hours, type:
0 */12 * * * /scripts/test.sh
2. Run cron job every 5 minutes
Sometimes you need to monitor your system every n minutes. Use the following expression to run the task every 5 minutes.
*/5 * * * * /scripts/test.sh
3. Run cron job every day at 2 am
Everyday backup can be scheduled for a specific time. The following expression runs the cron job at 2 am every day.
0 2 * * * /script/test.sh
To schedule a cron job every day at 2 pm, cron use 24 hours clock.
0 14 * * * /script/test.sh
4. Run cron job every day at 12:30 am
30 12 * * * /script/test.sh
5. Run cron job every minute
For example, you have a script that needs to be run every minute, use the following expression.
* * * * * /scripts/test.sh
6. Run cron job at certain minutes
Use the following cron expression to run the script at minutes 2, 15, and 30.
2,15,30 * * * * /scripts/test.sh
7. Run cron job to run every Saturday at 1 AM
This helps you nee to run the backup on Saturday at a specific time. The following expression helps to cron job on every Saturday at 1 AM.
0 1 * * sat /scripts/test.sh
8. Run cron job to run on specific Months
To run cron jobs at 01:00 on the 1st day of the month on January, April, and June.
0 1 1 jan,apr,jun * /script/test.sh
9. Run cron job on selected days
To run cron jobs on selected days such as Saturday and Sunday, use:
0 4 * * sat,sun /script/test.sh
10. Run cron jobs at certain hours of the day
At minute 0 past every hour from 9 through 17
0 9-17 * * * /script/test.sh
11. Run cron job twice on every Saturday and Sunday
Cron jobs allow running tasks on specific hours on specific days
0 2,23 * * sat,sun /scripts/test.sh
12. Run cron job for a range of Monday to Friday
At 02:00 on every day-of-week from Monday through Friday.
0 2 * * mon-fri /scripts/test.sh
13. Run cron job monthly
Instead of using 0 0 1 * *, you can use shortcut using @monthly. It runs at 00:00 on the 1st day of month. Alternatively you can place your script under /etc/cron.monthly/.
14. Run cron job weekly
Instead of using 0 0 * * mon, you can use shortcut using @weekly. It runs at 00:00 on Monday. Alternatively you can place your script under /etc/cron.weekly/.
15. Run cron job daily
Instead of using 0 0 * * *, you can use shortcut using @daily. It runs at 00:00 every day. Alternatively you can place your script under /etc/cron.daily/.
16. Run cron job hourly
Instead of using 0 */1 * * *, you can use shortcut using @hourly. It runs at 0 minutes past every hour. Alternatively you can place your script under /etc/cron.hourly/.
17. Run cron job on reboot
To run a task at every system reboot use @reboot string. The script will execute after rebooting.
18. Run cron job every 30 seconds
Cron only allows to run for a minimum of 60 seconds. There is no straightforward method to schedule a job to run every 30 seconds.
But we can use a work around by increasing the delay by 30 seconds.
* * * * /scripts/run.sh * * * * sleep 30; /scripts/run.sh
19. Run a cron job and redirect the output
This will run the php script every 3 minutes and redirect the output to a file.
*/3 * * * * /usr/bin/php /script/run.php >> /var/log/run.log
20. Run cron job to execute multiple commands
To run cron to execute multiple commands, use the operator && between the commands.
0 */12 * * Sun command1 && command2
Backup cron jobs
To backup cron, use the following command
# crontab -l > /backup/cron/root-bckup.txt
To backup cron of a specific user, type:
# crontab -u userName -l > /backup/cron/user-bckup.txt
To restore cron from the backup file, simple use:
# crontab /backup/cron/root-backup.txt
In this tutorial we learned about crontab in Linux and examples to schedule cron jobs.
If you have any feedback and suggestion please comment below.