Ict-innovation/LPI/107.2

= 107.2 Automate System Administration Tasks = Candidates should be able to use cron or anacron to run jobs at regular intervals and to use at to run jobs at a specific time.

Key Knowledge Areas


 * Manage cron and at jobs.
 * Configure user access to cron and at services.

1. Cron Jobs
A cron is a time-scheduled job on the UNIX system. For any administrative tasks that have to be run regularly, such as back-ups and Network Services, the cron facility is the best way to do it. This section is about the cron, anacron and other such facilities.

The cron facility consists of the crond daemon and a set of tables – crontabs - describing what work is to be done, when and how frequently. The daemon, which is started by init, wakes up every minute and checks the crontabs to determine what is to be done. Users manage crontabs using the crontab command.

Cron jobs can be run as shell scripts, and are better designed to accept no parameters.

crontabs

To create a crontab, the crontab command with the -e (for "edit") option will open a text editor where your specifications of the cron job can be specified. Every crontab entry will contain six fields:

Minute, hour, day of the month, month of the year, day of the week and String to be executed by sh.

The respective ranges for the time fields are: 0-59, 0-23, 1-31 and 1-12, 0-6 (Sunday=0).

NOTE: For all time fields, you can specify a range, or a single unit of time. You can specify the day of the week, using its short name: sun, mon, tue, and so on.

The final field will always be interpreted as a string to pass to the Bash.

Crontabs can use special characters, like % = newline, but these have to be preceded with a backslash (\). The line up to the first % is passed to the shell, while any line(s) after the % are passed as standard input.

Crontab Example:

Here, the back up shell script is executed at the 0th and 30th minutes (every 30 minutes), for the hours between 8 P.M and 11 P.M. every monday to Friday, during August. See the man page for crontab(5) for details on additional ways to specify times./ For further details on how to specify times please see the man page for crontab.

Cron jobs output can is usually sent as an email to the user who set up the cron job. Normally, like other system processes, the syslog facility will capture whatever the cron job did. Below is an example of a mailed report.

Location of the crontabs

All crontabs are stored in /var/spool/cron/*name of the user who created it*. Thus crontab is a suid program it can only run by a user with root authority.

The cron facility checks some more directories as well i.e. /etc/crontab and /etc/cron.d. As these are not user but system crontabs they have one more field between "day" and "command". This field just defines for which user the command should be run (root in normal case).

/etc/crontab example

In this case, the cron jobs are done by the run-parts command, whereas the /etc/crontab simply controls the timing.

NOTE: Note also that the crontab can include shell variables that have been set before the commands are run. All these cronjobs are run for the user root.

Anacron - for non 24/7 machines

Whereas the cron facility is ideal on computer systems that run continuously, and as such jobs that run hourly, some systems may require to be turned off every now and then. Consequently, you can only schedule jobs that run daily, weekly or monthly. A facility to do this is called anacron (for "anachronistic cron").

Anacron will keep timestamp files in /var/spool/anacron as a way to record when jobs are run. Anacron works by checking to see if the required number of days has passed since the job was last run and then determines to run it if necessary. The anacrontab is stored in /etc/anacrontab, and may contain environment variables. Every anacron job has four fields.

Time – Delay – Job-identifier - Command

The time is always a number of days, but can be specified as @weekly, @monthly to ensure that a job runs only then. The delay is the number of minutes to wait after the job is due to run before actually starting it. This feature can be useful if you do not want to flood the system with multiple jobs! You can use this to prevent a flood of jobs when a system first starts.

NOTE – Unlike the specific jobs contained in them, both /etc/crontab and /etc/anacrontab – can only be updated by direct editing, not by crontab -e

Sometimes you just want to run a job once. Linux provides the at command. The instructions to be executed are read from a file specified with the -f option, or from stdin if -f is not used. To display the time for the job to run, you can use the -v option. Below is an example:

Job output from at

The at command also has a -q option. When used, this option increases the nice value for the job. Be sure to look at the man pages for more details on these features.

Managing Jobs

You can use the crontab command with the -l option to list your crontab, and use the atq<tt> </tt>command to display the jobs you have queued using the at<tt> </tt>command, as shown in below.

Displaying scheduled jobs

NOTE – For details on the actual command scheduled for execution by at, you can use the -c<tt> </tt>option and the job number. You will notice that most of the environment that was active at the time the <tt>at </tt>command was issued is saved with the scheduled job.

Deleting Jobs

You can delete all scheduled cron jobs using the crontab<tt> </tt>command with the -r<tt> </tt>option as shown below:

You can delete system cron or anacron jobs, by editing the /etc/crontab, /etc/anacrontab, or by editing and/or deleting files in the /etc/cron.d directory.

For jobs scheduled using the at command, you can delete them using the atrm<tt> </tt>command with the job number. Multiple jobs have to be separated by a space.

Managing non-root user access

There may be situations when a non root user needs to have access to the crontab and the cron facility. In such a case, all you have to do is create the file /etc/cron.allow (if it does not already exist), and any non-root user must be listed in it. In contrasting effect, if /etc/cron.deny does exist, a non-root user who is listed in it cannot use crontab<tt> </tt>or the cron facility. If the /etc/cron.deny file is empty (and this is the default), all users are allowed to use the cron facility.

The following is a partial list of the files, terms and utilities that were used.


 * /etc/cron.{d,daily,hourly,monthly,weekly}
 * /etc/at.deny
 * /etc/at.allow
 * /etc/crontab
 * /etc/cron.allow
 * /etc/cron.deny
 * /var/spool/cron/*
 * crontab
 * at
 * atq
 * atrm

Previous Chapter | Next Chapter