Crontab

From aldeid
Jump to navigation Jump to search
This article is also available in one or more other languages
To view this article in French, click here
You might also see: cron

Description

Crontab is an interface that enables to edit cron tables, located in /var/spool/cron/crontabs. These tables don't have to be manually edited. They enable to plan jobs.

Usage

Basic usage is as follows:

crontab [-u user] file
crontab [-u user] { -e | -l | -r }

Options

-e
Edit <user>'s crontab
-l
List <user>'s crontab
-r
Purge crontab of <user>

Environment variables

To change default editor (nano), you can define $EDITOR environment variable as follows:

export EDITOR=/usr/bin/vim

To make this variable known each time you boot your machine, insert this line in your ~/.bashrc.

To avoid to accidentally remove crontab with the -r option, you can also define an alias to replace the command:

alias crontab="crontab -i"

The -i parameter enables to display a confirmation before deleting a crontab.

crontab syntax

Each line of a crontab must be formatted as follows, with a space or tab between each item:

24 3 * * 1 root /foo/bar/script.sh
0-59 0-23 1-31 1-12 0-7    ╰➙ Script to execute
 ╰➙ Command will be launched as root
 ╰➙ Day of week (1=Monday, 2=Tuesday, ..., 7=0=Sunday)
 ╰➙ Month (1=January, 2=February, ..., 12=December)
 ╰➙ Day of month
 ╰➙ Hour
 ╰➙ Minute

Examples

To launch a task every minute:

* * * * * root /every/minute/task.sh

To launch a task every 5 minutes:

*/5 * * * * root /every/fiveminutes/task.sh

To launch a task every Tuesday, at 04:30 AM :

30 4 * * 2 root /every/tuesday/task.sh