How To Set Cron Jobs In Linux - An Introduction to Crontab

Dec 13, 2019 7621 Gulfam
set-cron-jobs

Overview This article gives you a clear understanding of how to set cron jobs in Linux and Unix-like operating systems. Crontab stands for cron table utilizes the cron daemon to run a task at a regular interval of time. The cron table is a list of tasks scheduled to run regularly at a particular time. The cron task scheduler runs tasks in the background on a specific interval of time. Crontab is almost similar to task schedules in Windows.

Cron Jobs are very useful for the task which is repetitive in nature like daily backup and system maintenance etc. In this tutorial of crontab, we will read about how to set cron jobs in Linux using the crontab command in Linux.

Syntax of Crontab

The crontab has six fields [MIN HOUR DOM MON DOW CMD]. The first five fields are used to specify the time of the task and the last field is to specify the command or script to be executed.

# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .-------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .----- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  *  command to be executed

Crontab has three operators that are used to provide multiple values in crontab command.

  • Asterisk (*)specifies all possible values for a field

  • The comma (,)specifies a list of values

  • Dash (-)specifies a range of values

  • Separator (/): specifies a step value


How to Add/Edit Crontab

Just run the below command in the terminal to add or edit the jobs in Crontab. The command opens the crontab file where you can add your new jobs or update the existing one.

crontab -e

The above command updates the jobs in the crontab file of the current logged-in user. If you want to update the crontab jobs for another user then run the below command.

crontab -u username -e

Cron Options

Majorly there are two options that we are broadly used while working with crontab.

  • List all cron jobs

    1. crontab -l

    2. crontab -u username -l

  • Delete all crontab jobs

    1. crontab -r

    2. crontab -r -u username


Cron Job Examples

  • Run /scripts/backup.sh daily at 6 AM

    0 3 * * * /root/backup.sh

    0 – 0th Minute
    3 – 03 AM
    * – Every Day
    * – Every month
    * – Every day of the week

  • Run /scripts/backup.sh every minute

    * * * * * /root/backup.sh

    * – Every Minute
    * – Every hour
    * – Every Day
    * – Every month
    * – Every day of the week

  • Run /scripts/backup.sh twice daily

    00 11, 16 * * * /root/backup.sh

    00 – 0th Minute (Top of the hour)
    11,  16 – 11 AM and 4 PM
    * – Every Day
    * – Every month
    * – Every day of the week

  • Run /scripts/backup.sh on selected months

    * * * jan,may,aug * /root/backup.sh

    * – every Minute
    * – Every hour
    * – Every Day
    * – Jan, May and August
    * – Every day of the week

  • Run /scripts/backup.sh runs twice on every Sunday and Monday 

    0 4,17 * * sun,mon /scripts/script.sh

    * – every Minute
    * – 4 AM and 5 PM
    * – Every day
    * – Every month
    * – Sunday and Monday


Strings in Crontab

Irritated with numbers? But don’t worry there is a very simple method to set a cron job using string notations. Cron has some reserved string you can use them to set cron jobs quickly.

  • @hourly: Execute once every hour i.e. “0 * * * *

  • @daily ormidnight: Execute once every day i.e. “0 0 * * *

  • @weekly: Execute once every week, i.e. “0 0 * * 0

  • @monthly: Execute once every month i.e. “0 0 1 * *

  • @annually or @yearly: Execute once every year i.e. “0 0 1 1 *

  • @reboot: Execute once at every startup

For example, if you want to run your backup script on a daily basis then just add a job like this.

@daily /path/to/backup/script.sh

I hope this article answered most of your questions regarding cron jobs. If you have any queries or doubts, feel free to reach out to me in the comment box.

Happy creating cron jobs.

About The Author

author photo

Gulfam Ansari

A code lover, who also love to write about gadgets and new technologies.