Cron

From Wikipedia, the free encyclopedia

The correct title of this article is cron. The initial letter is shown capitalized due to technical restrictions.

cron is a time-based scheduling service in Unix and Unix-like operating systems. It is driven by a configuration file named crontab that specifies shell commands to run periodically on a given schedule. Early versions of cron, available up through Version 7 Unix and 32V made their services available only to the super-user of the operating system; this was the single-user version. With the release of Unix System V these services were extended to all account-holding users of the system, the multi-user cron.

Contents

[edit] Versions of cron

cron has been recreated several times in its history.

[edit] Up through Version 7 Unix

The cron in Version 7 Unix, written by Brian Kernighan, was a system service (what were later called daemons) invoked from /etc/cron when the operating system entered multi-user mode. Its algorithm was straightforward:

  1. Read /usr/lib/crontab
  2. Determine if any commands are to be run at the current date and time and if so, run them as the Superuser, root.
  3. Sleep for one minute.
  4. Repeat from step 1.

This version of cron was basic and robust, but it also consumed resources whether it found any work to do or not; upon hearing this description, Doug Comer, a professor at Purdue University, remarked, "Ah, an oblivious algorithm." In an experiment at Purdue University in the late 1970s to extend cron's service to all 100 users on a time-shared VAX it was found to place too much load on the system.

[edit] Multi-user Cron

The next incarnation of cron was created to extend the capabilities of cron to all users of a Unix system, not just the superuser. Though this may seem trivial today with most Unix and Unix-like systems having powerful processors and small numbers of users, at the time it required a new approach on a 1 MIP system having roughly 100 user accounts.

In the August, 1977 issue of the Communications of the ACM, W. R. Franta and Kurt Maly published an article entitled "An efficient data structure for the simulation event set" describing an event queue data structure for discrete event-driven simulation systems that demonstrated "performance superior to that of commonly used simple linked list algorithms," good behavior given non-uniform time distributions, and worst case complexity O(\sqrt{n}), "n" being the number of events in the queue.

A graduate student, Robert Brown, reviewing this article, recognized the parallel between cron and discrete event simulators, and created an implementation of the Franta-Maly event list manager (ELM) for experimentation. Discrete event simulators run in "virtual time," peeling events off of the event queue as quickly as possible and advancing their notion of "now" to the scheduled time of the next event. By running the event simulator in "real time" instead of virtual time, a version of cron was created that spent most of its time sleeping, waiting for the moment in time when the task at the head of the event list was to be executed.

The following school year brought new students into the graduate program, including Keith Williamson, who joins the systems staff in the Computer Science department. As a "warm up task" Brown asked him to flesh out the prototype cron into a production service, and this multi-user cron went into use at Purdue in late 1979. This version of cron wholly replaced the /etc/cron that was in use on the Computer Science department's VAX 11/780 running 32/V.

The algorithm used by this cron is as follows:

  1. On start-up, look for a file named .crontab in the home directories of all account holders.
  2. For each crontab file found, determine the next time in the future that each command is to be run.
  3. Place those commands on the Franta-Maly event list with their corresponding time and their "five field" time specifier (see Crontab).
  4. Enter main loop:
    1. Examine the task entry at the head of the queue, compute how far in the future it is to be run.
    2. Sleep for that period of time.
    3. On awakening and after verifying the correct time, execute the task at the head of the queue (in background) with the privileges of the user who created it.
    4. Determine the next time in the future to run this command and place it back on the event list at that time value.

Additionally, the daemon would respond to SIGHUP signals to rescan modified crontab files and would schedule special "wake up events" on the hour and half hour to look for modified crontab files. Much detail is omitted here concerning the inaccuracies of computer time-of-day tracking, Unix alarm scheduling, explicit time-of-day changes, and process management, all of which account for the majority of the lines of code in this cron. This cron also captured the output of stdout and stderr and e-mailed any output to the crontab owner.

The resources consumed by this cron scale only with the amount of work it is given and do not inherently increase over time with the exception of periodically checking for changes.

Williamson completed his studies and departed the University with a Masters of Science in Computer Science and joined AT&T Bell Labs in Murray Hill, New Jersey, and took this cron with him. At Bell Labs, he and others incorporated the Unix at command into cron, moved the crontab files out of users' home directories (which were not host-specific) and into a common host-specific spool directory, and of necessity added the crontab command to allow users to copy their crontabs to that spool directory.

This version of cron later appeared largely unchanged in Unix System V and in BSD and their derivatives, the Solaris Operating System from Sun Microsystems, IRIX from Silicon Graphics, HP-UX from Hewlett-Packard, and IBM AIX (operating system). Technically, the original license for these implementations should be with the Purdue Research Foundation who funded the work, but this took place at a time when little concern was given to such matters.

[edit] Linux crons

With the advent of the GNU Project and the Linux new crons appeared, many described in separate articles. The most prevalent of these is the Vixie cron followed by anacron and fcron. Simple code inspection suggests that these implementations use the original cron's "oblivious" algorithm but with memory size, processor speed, and Linux systems typically not supporting scores of users, this matters little.

In other languages