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.
Practical Task Scheduling Deployment
July 20, 2016 12:00 pm CDT
One of the best things about the UNIX environment (aside from being stable and efficient) is the vast array of software tools available to help you do your job. Traditionally, a UNIX tool does only one thing, but does that one thing very well. For example, grep is very easy to use and can search vast amounts of data quickly. The find tool can find a particular file or files based on all kinds of criteria. It's pretty easy to string these tools together to build even more powerful tools, such as a tool that finds all of the .log files in the /home directory and searches each one for a particular entry. This erector-set mentality allows UNIX system administrators to seem to always have the right tool for the job.
Cron traditionally has been considered another such a tool for job scheduling, but is it enough? This webinar considers that very question. The first part builds on a previous Geek Guide, Beyond Cron, and briefly describes how to know when it might be time to consider upgrading your job scheduling infrastructure. The second part presents an actual planning and implementation framework.
Join Linux Journal's Mike Diehl and Pat Cameron of Help Systems.
Free to Linux Journal readers.Register Now!
- SUSE LLC's SUSE Manager
- My +1 Sword of Productivity
- Murat Yener and Onur Dundar's Expert Android Studio (Wrox)
- Non-Linux FOSS: Caffeine!
- Managing Linux Using Puppet
- Doing for User Space What We Did for Kernel Space
- Tech Tip: Really Simple HTTP Server with Python
- Parsing an RSS News Feed with a Bash Script
- SuperTuxKart 0.9.2 Released
- Rogue Wave Software's Zend Server
With all the industry talk about the benefits of Linux on Power and all the performance advantages offered by its open architecture, you may be considering a move in that direction. If you are thinking about analytics, big data and cloud computing, you would be right to evaluate Power. The idea of using commodity x86 hardware and replacing it every three years is an outdated cost model. It doesn’t consider the total cost of ownership, and it doesn’t consider the advantage of real processing power, high-availability and multithreading like a demon.
This ebook takes a look at some of the practical applications of the Linux on Power platform and ways you might bring all the performance power of this open architecture to bear for your organization. There are no smoke and mirrors here—just hard, cold, empirical evidence provided by independent sources. I also consider some innovative ways Linux on Power will be used in the future.Get the Guide