cron: Job Scheduler
The cron daemon, crond, packaged with most Linux distributions, controls scheduling of regularly occurring jobs. When started upon entry into multi-user mode, crond scans the directories /var/spool/cron/crontabs and /etc/cron.d and the file /etc/crontab for work to do. crond then awakens every minute, performs the work its record of jobs says it should do at that time, mails the output (by default) to the owning user, then sleeps until the beginning of the next minute.
The implementation of crond packaged with Debian 2.0, the distribution I used when writing this article, carries the name Vixie Cron, after Paul Vixie, its author. I will use “cron” to refer, variously, to both the crond process and the cron facility.
cron evolved to enable the execution of jobs at regular intervals. Have you had occasion to use the log files in /var/log? Most Linux distributions come with a ready set of cron jobs to tame those log files. Without cron jobs, the file system holding /var would eventually fill completely with log files. The potential uses for cron exceed the small customizations I have made to my home environment. If you want to automate something that runs more than once, turn to cron.
Individual users may use cron to automate tasks. Normally, all users may make use of cron. If superuser has created /etc/cron.allow or /etc/cron.deny, then access to the cron facility depends on the contents of those files. If /etc/cron.allow exists, your user name must appear in it for you to use cron. If /etc/cron.deny exists but /etc/cron.allow does not, your user name must not appear in /etc/cron.deny, or cron will not work for you. To edit your cron settings, use the crontab command:
This will create a cron table, or “crontab file”, which cron will read to find work. The crontab command looks first for the VISUAL environment variable, then for the EDITOR environment variable. It will use the editor named in those variables to provide editing of crontab files. Without one of these environment variables set, Debian 2.0's crontab uses the ae editor. Other distributions may have a different default behavior for crontab. Make the changes you desire, save the file and exit the editor.
Why do we not edit the crontab file directly? The reason is cron requires a specific format for its job entries. The crontab command performs syntax checking before allowing a newly edited crontab file to enter circulation. If the new crontab has a syntax error, crontab complains and asks if you want to edit again. To protect the crontab files, the crontab command makes root the owner of the crontab files.
To view your newly edited crontab file, use this command:
The output should look something like Listing 1. Each crontab entry provides either an environment variable or a time-specific cron command. cron sets a few environment variables automatically. Others, such as MAILTO, can be set by the user. Normally, cron mails the output of each cron job to its owner. If you put the line
MAILTO="fred"in your crontab file, the output of your cron jobs would go to user fred instead. More likely, you would want to suppress cron output. If you set MAILTO to null,
MAILTO=""then cron will discard the job output.
The fields in a time-specific cron command appear in this order: minute (0-59 allowed), hour (0-23 allowed), day of month (1-31 allowed), month (1-12 or names allowed), day of week (0-7 or names allowed, with both 0 and 7 representing Sunday), and the command to run. The numerical fields also allow ranges of numbers, wild cards, lists and methods for running cron jobs at every Nth interval, such as every third hour. The asterisk character works as a wild card, representing every occurrence of the field's value. For details, see the crontab(5) manual page.
The example below will run the ls command every minute of the noon hour on the first day of the month, discarding the output:
MAILTO="" # Minute Hour Day-Of-Month Month Day-Of-Week # Command * 12 1 * * /usr/bin/ls
This next example will run the free command every other hour and mail the output to fred:
MAILTO="fred" * */2 * * * /usr/bin/freeThe system-wide crontab, stored in the file /etc/crontab, provides a slightly different method for running cron jobs. It does not have a special editor, so you must take extra care when editing it. In addition, it provides a user name field between the Day-of-Week and Command fields, to run jobs under a user ID other than root, without having to create a separate crontab file for that user. Edit it with your favorite editor and save the changes; cron will automatically update its job list.
- One Port to Rule Them All!
- Privacy Is Personal
- PHP for Non-Developers
- Linux Kernel 4.1 Released
- Secure Server Deployments in Hostile Territory
- July 2015 Issue of Linux Journal: Mobile
- Django Templates
- Comprehensive Identity Management and Audit for Red Hat Enterprise Linux
- A Code Boot Camp for Underprivileged Kids
- Attack of the Drones