Schedule Tasks with Crontab

The crontab is used for running specific tasks on a regular interval on Linux and macOS operational systems. Crontab is similar to Windoze's Task Scheduler. Crontab is very useful for routine tasks like scheduling system scanning, daily backups etc.

Crontab executes jobs automatically in the backend on a specified time and interval. You can also use crontab to run tasks only once.

Export the Default Editor

First things first, if you haven't done by now set the default editor for Terminal. Most people use vi but for the first time user nano is easier. Set the default editor like this:

export EDITOR='nano'
If you want to retain this configuration when you close the Terminal then add this line to your .bashrc, .profile, .bash_profile or whatever initialization script your shell uses.

Linux Crontab Syntax

Linux crontab has six fields. 1-5 fields defines the date and time of execution. The 6th field is used for command or script to be executed. The crontab syntax are as following:

<Minute>   <hour>   <Day_of_the_Month>   <Month_of_the_Year>   <Day_of_the_Week>   <Command>

Crontab

Value Operators

*   any value
,   value list separator (1,2,3,6,9)
-   range of values (2-9)
/   step values (*/10 that is every 10... minutes, hours etc)

Add/Edit Crontab

To add or update job in crontab, use below command. It will open crontab file in the editor where a job can be added/updated.
crontab -e
By default, it will edit crontab entries of current logged in user. To edit other user crontab use this command:
crontab -u username -e

Saving Crontab

When you finish editing your cron script save it and exit the editor, after that the rules will be in effect. The way to do that with nano is to press CTRL+O to save and CTRL+X to exit.

List Crontab

To list your user's crontab list type:
crontab -l

Examples

Execute Twice a Day, at 4AM and 1PM:

0 4,13 * * * /path/script.sh

Execute on Every Minute:

* * * * *  /path/script.sh

Execute Every 10 minutes:

*/10 * * * * /path/monitor.sh

Execute on Selected Months:

* * * jan,may,aug *  /path/script.sh

Execute at 2 PM on Selected Days:

0 14 * * sun,fri  /path/script.sh

Execute on the First Sunday of Every Month at 2AM:

0 2 * * sun  [ $(date +%d) -le 07 ] && /path/script.sh

Execute Every Four Hours:

0 */4 * * * /path/script.sh

Execute Twice Every Sunday and Twice Every Monday at 4AM and 2PM:

0 4,14 * * sun,mon /path/script.sh

Execute Every 30 Seconds:

* * * * * /path/script.sh
* * * * * sleep 30; /path/script.sh

Multiple Tasks in Single Cron:

* * * * * /path/script.sh; /path/script2.sh

Using Simple Timestamps:

@yearly /path/script.sh
@monthly /path/script.sh
@weekly /path/script.sh
@daily /path/script.sh
@hourly /path/script.sh
And @reboot to run on the system startup:
@reboot /path/script.sh

Backup Current Cron List:

crontab -l > cron_backup.txt

Restore Cron List from Backup:

crontab cron_backup.txt

Remove All Cron Jobs:

crontab -r

You can use this easy to use cron editor hosted at https://crontab.guru.
{{ message }}

{{ 'Comments are closed.' | trans }}