ArticleslgStudy

computer science

Cron

Cron is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Cron rather than just read about it. In short: cron is a time-based job scheduler. A scheduled job is known as a cron job.

Cron — main illustration
Cron — illustration

Key takeaways

  • Cron belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Cron to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Cron from memory before moving on to harder problems.

Reference excerpt

cron is a time-based job scheduler. A scheduled job is known as a cron job. Although typically used to automate system maintenance and administration it can be used to automate any task. cron is most suitable for scheduling repetitive tasks, as scheduling a one-time task can be accomplished via at. The daemon name originates from Chronos, the Greek word for time. cron is generally available on Unix-like operating systems.

Overview The actions of cron are driven by a crontab (cron table) file. The crontab files are stored where the lists of jobs and other instructions to the cron daemon are kept. Users can have their own individual crontab files and often there is a system-wide crontab file (usually in /etc or a subdirectory of /etc e.g. /etc/cron.d) that only system administrators can edit. Each line of a crontab file represents a job, and looks like this:

* * * * * <command to execute> # | | | | | # | | | | day of the week (0–6) (Sunday to Saturday; # | | | month (1–12) 7 is also Sunday on some systems) # | | day of the month (1–31) # | hour (0–23) # minute (0–59)

The syntax of each line expects a cron expression made of five fields which represent the time to execute the command, followed by a shell command to execute. While normally the job is executed when the time/date specification fields all match the current time and date, there is one exception: if both "day of month" (field 3) and "day of week" (field 5) are restricted (do not contain "*"), then one or both must match the current day. For example, the following clears the Apache error log at one minute past midnight (00:01) every day, assuming that the default shell for the cron user is Bourne shell compliant:

This example runs a shell program called export_dump.sh at 23:45 (11:45 PM) every Saturday.

Note: On some systems it is also possible to specify */n to run for every n-th interval of time. Also, specifying multiple specific time intervals can be done with commas (e.g., 1,2,3). The line below would output "hello world" to the command line every 5th minute of every first, second and third hour (i.e., 01:00, 01:05, 01:10, up until 03:55). The configuration file for a user can be edited by calling crontab -e regardless of where the actual implementation stores this file. Some cron implementations, such as the popular 4th BSD edition written by Paul Vixie and included in many Linux distributions, add a sixth field: an account username that runs the specified job (subject to user existence and permissions). This is allowed only in the system crontabs—not in others, which are each assigned to a single user to configure. The sixth field is alternatively sometimes used for year instead of an account username—the nncron daemon for Windows does this. The Amazon EventBridge implementation of cron does not use 0 based day of week, instead it is 1-7 SUN-SAT (instead of 0-6), as well as supporting additional expression features such as first-weekday and last-day-of-month.

Nonstandard predefined scheduling definitions Some cron implementations support the following non-standard macros:

@reboot configures a job to run once when the daemon is started. Since cron is typically never restarted, this typically corresponds to the machine being booted. This behavior is enforced in some variations of cron, such as that provided in Debian, so that simply restarting the daemon does not re-run @reboot jobs. @reboot can be useful if there is a need to start up a server or daemon under a particular user, and the user does not have access to configure init to start the program.

Cron permissions These two files play an important role:

/etc/cron.allow – If this file exists, it must contain the user's name for that user to be allowed to use cron jobs. /etc/cron.deny – If the cron.allow file does not exist but the /etc/cron.deny file does exist then, to use cron jobs, users must not be listed in the /etc/cron.deny file. Note that if neither of these files exists then, depending on site-dependent configuration parameters, either only the super user can use cron jobs, or all users can use cron jobs.

Time zone handling Most cron implementations simply interpret crontab entries in the system time zone setting that the cron daemon runs under. This can be a source of dispute if a large multi-user machine has users in several time zones, especially if the system default time zone includes the potentially confusing DST. Thus, a cron implementation may as a special case recognize lines of the form "CRON_TZ=<time zone>" in user crontabs, interpreting subsequent crontab entries relative to that time zone.

History

Early versions The cron in Version 7 Unix was a system service (later called a daemon) invoked from /etc/rc when the operating system entered multi-user mode. Its algorithm was straightforward:

Read /usr/lib/crontab Determine if any commands must run at the current date and time, and if so, run them as the superuser, root. Sleep for one minute 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. 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.

… excerpt ends here. Continue reading the full article.

Illustrations

Cron illustration

Worked examples

Example 1 — a first encounter with Cron

Start with the simplest possible case. Write down what Cron claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Cron before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Cron ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Cron

In research
Cron appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Cron in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Cron is common in secondary-school and first-year university syllabi. It links to neighbouring topics Job scheduling, Standard Unix programs, Unix SUS2008 utilities, so understanding it makes those chapters shorter.
In everyday life
Look for Cron outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Cron” →

How to study Cron in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Cron means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Cron out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Cron in simple terms?

cron is a time-based job scheduler. A scheduled job is known as a cron job.

Why does Cron matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Cron?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Cron.

Tags

  • Job scheduling
  • Standard Unix programs
  • Unix SUS2008 utilities
  • Unix process- and task-management-related software

Keep exploring