Table of Content

107.2 Automate system administration tasks by scheduling jobs

Weight: 4

Description: 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
  • Configure anacron

Terms and Utilities:

/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
anacron
/etc/anacrontab

cron jobs

root@dclab:/etc# ll|grep cron
-rw-r--r-- 1 root root 401 Dec 28 2014 anacrontab
drwxr-xr-x 2 root root 4096 Apr 22 2015 cron.d/
drwxr-xr-x 2 root root 4096 Sep 14 06:41 cron.daily/
drwxr-xr-x 2 root root 4096 Apr 22 2015 cron.hourly/
drwxr-xr-x 2 root root 4096 Apr 22 2015 cron.monthly/
-rw-r--r-- 1 root root 722 Oct 27 2014 crontab
drwxr-xr-x 2 root root 4096 Sep 13 08:56 cron.weekly/

cron config

/var/spool/cron/
/etc/cron.d/
/etc/crontab

02 4 * * * root run-parts /etc/cron.daily

The minute of the hour (0–59) */10 every 10 minutes
The hour of the day (0–23) */2 every 2 hours
The day of the month (1–31)
The month of the year (1–12)
The day of the week (0–7)

user cron job

crontab [-u user] [-l | -e | -r] [file]

oldhorse@dclab:~$ crontab cronjob // will overwrite existing crontab
oldhorse@dclab:~$ crontab -l
SHELL=/bin/bash
MAILTO=oldhorse
00 12 * * * /sbin/ip -s link

cron permission

/etc/cron.allow
/etc/cron.deny

anacron

designed as a supplement to cron to ensure that regular maintenance jobs are executed at reasonable intervals

oldhorse@dclab:~$ cat /etc/anacrontab
# /etc/anacrontab: configuration file for anacron

# See anacron(8) and anacrontab(5) for details.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
HOME=/root
LOGNAME=root

# These replace cron's entries
1 5 cron.daily run-parts --report /etc/cron.daily
7 10 cron.weekly run-parts --report /etc/cron.weekly
@monthly 15 cron.monthly run-parts --report /etc/cron.monthly

The second field, delay, is a delay period, in minutes, between the time anacron starts and the time the command is run

oldhorse@dclab:~$ cat /etc/crontab
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#

at

Time of Day HH:MM, optionally followed by AM or PM
Time Keywords noon, midnight, or teatime(4pm)
Day Specification MMDDYY, MM/DD/YY, or DD.MM.YY
A Specified Period in the Future now + 2 hours to run a job in two hours

oldhorse@dclab:~$ at now +1 hour
warning: commands will be executed using /bin/sh
at> ip -s link
at> 
job 1 at Fri Mar 11 09:21:00 2016
oldhorse@dclab:~$ atq
1 Fri Mar 11 09:21:00 2016 a oldhorse

/etc/at.allow and /etc/at.deny
- If neither at.allow nor at.deny exists, only root may use the at command.
- If at.allow exists, the users it lists may use the at command.
- If at.deny exists, everybody except those mentioned in this file may use the at command.