Cron: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
Line 130: Line 130:
  * * * * * /home/jmon/bin/es-cron
  * * * * * /home/jmon/bin/es-cron


/etc/crontab
/etc/crontab:


  * * * * * root /home/jmon/bin/es-cron
  * * * * * root /home/jmon/bin/es-cron

Latest revision as of 20:43, 15 February 2018

Internal

Overview

cron wakes up periodically and executes tasks specified in /etc/crontab and in all users' crontab. The users's crontabs must be explicitly registered with cron, to be executed.

Installation

yum install crontabs

Simplest cron Job

Try first to schedule it as a regular user, not root.

For that, the user needs to be allowed to schedule cron jobs. One way to allow it, is to add the user in /etc/cron.allow.

Example:

root
jmon

Then list the content of the user's crontab:

crontab -l

Normally, it should not be anything there.

Then, create a crontab file using the rules described below (crontab Format). Put it in a conventional place (~/etc, for example).

Then, "install" the crontab file - the change will take place instantaneously:

crontab /home/jmon/etc/crontab

To deactivate it, "delete" the crontab:

crontab -r

Configuration

The default configuration file is /etc/crontab.

Working Directory

If not otherwise configured, when a cron job is executed the working directory is the home directory of the users the cron job is executed on behalf of.

cron Jobs' Output

By default, the cron daemon sends the content of stdout/stderr of all jobs it executes to mail. However, in some cases, such as running cron jobs in containers, this may be impractical, so the output can be redirected to local file for troubleshooting.

Mail

When executing commands, any output is mailed to the owner of the crontab (or to the user named in the MAILTO environment variable in the crontab, if such exists). By any output we mean both stdout AND stderr output.

cron will also look at MAILTO variable when it needs to mail the result of the command. If MAILTO is defined and non empty, mail will be sent to the user defined by it. If MAILTO is defined and empty, no e-mail will be sent. If MAILTO is not defined, cron will send e-mail to the owner of the crontab.

Unfortunately, I did not find an easy way to modify these e-mails' subject line, so the common solution is to:

1. The script executed by cron does not output anything at stdout.

2. Instead, it invokes the mail program itself with -s argument.

Local File

The stdout/stderr can be captured and redirected to a local file, so nothing will be mailed:

* * * * * /some-job >> /tmp/cron.log 2>&1

crontab Format

A crontab file contains instructions to the cron daemon of the general form: "run this command, at this time on this date". Jobs specified in /etc/crontab, which may contain system-level jobs that can be executed as any user, should also specify an user to execute as. Each user has their own crontab, and commands in any given crontab will be executed as the user who owns the crontab.

crontab entries are examined once every minute.

Blank lines, leading spaces and tabs, and lines whose first non-space character is a pound-sign (#) -comments- are ignored. Comments are not allowed on the same line as cron commands, since they will be taken to be part of the command. Similarly, comments are not allowed on the same line as environment variable settings.

An active line in a crontab will be either an environment setting or a cron command.

cron Commands

Each line has five time and date fields, followed by a user name if this is the system crontab file, followed by a command.

Commands are executed by cron when the minute, hour, and month of year fields match the current time, and at least one of the two day fields (day of month, or day of week) match the current time. The time and date fields are the following:

              field          allowed values
              -----          --------------
              minute         0-59
              hour           0-23
              day of month   1-31
              month          1-12 (or names, see below)
              day of week    0-7 (0 or 7 is Sun, or use names)

A field may be an asterisk (*), which always stands for "first-last".

Ranges of numbers are allowed. Ranges are two numbers separated with a hyphen. The specified range is inclusive. For example, 8-11 for an "hours" entry specifies execution at hours 8, 9, 10 and 11.

Step values can be used in conjunction with ranges. Following a range with "<number>" specifies skips of the "number" value through the range. For example, "0-23/2" can be used in the hours field to specify command execution every other hour (the alternative in the V7 standard is "0,2,4,6,8,10,12,14,16,18,20,22"). Steps are also permitted after an asterisk, so if you want to say "every two hours", just use "*/2".

Lists are allowed. A list is a set of numbers (or ranges) separated by commas. Examples: "1,2,5,9", "0-4,8-12".

Names can also be used for the "month" and "day of week" fields. Use the first three letters of the particular day or month (case does not matter). Ranges or lists of names are not allowed.

The "sixth" field (the rest of the line) specifies the command to be run. The entire command portion of the line, up to a newline or % character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the cronfile. Percent-signs (%) in the command, unless escaped with backslash (\), will be changed into newline characters, and all data after the first % will be sent to the command as standard input.

Environment Variables

An environment setting looks like this:

name=value

Several environment variables are set up automatically by the cron daemon:

  • SHELL is set to /bin/sh (the value may be overridden by settings in crontab)
  • LOGNAME and HOME are set from the /etc/passwd line of the crontab's owner (HOME may be overridden by settings in crontab, LOGNAME may not).

cron Inside a Container

Run a Cron Job Inside of a Docker Container

Examples

Script Run Every Minute

User's crontab:

* * * * * /home/jmon/bin/es-cron

/etc/crontab:

* * * * * root /home/jmon/bin/es-cron

Run a Script On the Hour

It'll run at HH:00. In the user's crontab, this is specified as:

0 * * * * /home/jmon/bin/es-cron

Run a Script Every Morning at 3:00 AM

In the user's crontab, this is specified as:

0 3 * * * /home/jmon/bin/es-cron