Crontab: Usage
Introduction
In short: crontab is used to execute periodic tasks automatically. In detail: crond is a daemon that runs on most Linux distributions and manages certain periodic tasks. A cronjob is a periodic task defined by the user, which will be executed by the system (or predefined by the authors of the Linux distribution you’re using).
Let’s consider a simple example: we want to send ourselves the same email every morning…
Moi@machine:~$ echo "Life is beautiful!"
At this stage, we’re only at the drafting stage of the email and nothing is automated yet. To automate a task, you need to indicate it to cron in specific files called “crontabs”; each user has a crontab that they manage as they wish. To access your crontab, type:
~$ crontab -e
A command line in this file consists of 6 fields, separated by spaces or tabs, in this order:
The syntax of the fields may appear differently depending on your distribution; for example, you might find jj or dom (day of month) for the day of the month, mon for month, etc. Multiple elements in the same field are separated by a comma (e.g., 1,3,5 in the month field means “January, March, May”); similarly, a range is expressed by a hyphen (e.g., 1-3 for the day of the week means “Monday to Wednesday”); an asterisk (*) designates the largest possible interval.
In our example, if we want to send ourselves an email every day at 8:01 AM, we’d type:
01 8 * * * echo "Life is beautiful!"
If we want to execute a script on the first day of every month at 5:42 AM:
42 5 1 * * monscript.sh
Or make a backup Monday through Friday, every day at 11:59 AM:
59 11 * * 1-5 backup.sh
The primary purpose of cron is actually to manage system administration tasks. Since these are generally repetitive, it’s quite useful to have such a task management system at your disposal… The main configuration file for cron is usually /etc/crontab
. There’s a small difference with the syntax of user crontabs; since this command file is executed as root, there’s an option to specify a user other than root for executing scheduled tasks.
The syntax of the commands is therefore as follows:
minutes hours day-of-month month day-of-week user command
For example:
7 20 * * * root echo "This command is executed every day as root at 8:07 PM"
7 20 * * * bob echo "This command is executed every day by user bob at 8:07 PM"
The rest of the syntax should be familiar to you now.
Controlling Cron Usage
The use of cron can lead to mobilization of system resources, particularly if administrative tasks are heavy and numerous (e.g., 50 users having set up a cronjob that runs every minute can use up quite a bit of system resources…). To avoid this, cron integrates usage authorization management. Two files are used: /etc/cron.deny
and /etc/cron.allow
.
The syntax is very simple and identical to other daemons that work with .allow and .deny files: to prohibit a particular user from using cron, simply enter their name in the cron.deny file. For example:
~$ echo "bob" >> /etc/cron.deny
This will simply prohibit bob from using cron. To only authorize certain specifically designated users, enter the command:
~$ echo "ALL" >> /etc/cron.deny && echo "bob" >> /etc/cron.allow
… and only bob will be authorized to use cron.
Note: if neither of the two files exists, only the super user (root) will have the right to use cron. Additionally, an empty /etc/cron.deny
file means that all users can use cron.
The crond process is normally launched at system startup. To verify this:
~$ ps aux
Good, it seems to be running! You can also view it in the graphical service manager of your desktop environment. If the service is not started, check the corresponding box (via the graphical interface) or enter the command:
/etc/rc.d/init.d/crond start
or /etc/init.d/crond start
or even /etc/init.d/cron start
(depending on the distribution used…).
Backing Up the Crontab
I’ve been looking for this folder for a long time!!! There’s a folder where all user crontabs are stored:
/var/spool/cron/crontabs
And for daily / hourly / monthly… it’s in this folder:
/etc/cron.d
Configuring Email for a Crontab
If you want a crontab to redirect all output to a specific address, add this line:
MAILTO="user@fqdn"
Last updated 01 Aug 2011, 05:53 CEST.