Automation
Overview
Today we will see two tools that can be used to schedule tasks to run at specific, repeatable times. These can be used to automate processes such as the following:
- Sending mail
- Rotating log files
- Running batch jobs
- Checking for updates
- Backing up data
Cron
The cron tool is used to run commands at specific times. The
crond daemon runs and monitors the cron tab file(s) which specify
which commands to run at which times. When the time comes, the associated
commands will be run.
Cron files are stored in /var/spool/cron/, but are most
commonly edited with the crontab -e command which pulls up
an editor and checks the syntax afterwards (like the visudo command).
Cron jobs are stored in a single line in the file, where fields 1-5 are used for scheduling and the sixth field is the command to be run. The first 5 fields have the following meaning:
- Minute of the hour (0-59)
- Hour of the day (0-23)
- Day of the month (1-31)
- Month of the year (1-12)
- Day of the week (0-6 where Sunday is 0)
If we place a * in a field, it means it will be run for every of those
time frames. For example, a * in the month of the year field means it runs for every
month (at the given time specified).
The following are examples of cron jobs:
# every Monday at noon, delete all temp files older than one week 0 12 * * 1 find /tmp -mtime +7 -type f | xargs rm # first day of every month at midnight, optimize mysql databases 0 0 1 * * mysqlcheck -u maintenance --optimize --all-databases # every hour, run a custom script 0 * * * * /usr/local/bin/health-check.sh
With cron, if the machine is not on when the time for a task
transpires, the task is not run. It doesn't check for tasks that were missed
when the machine boots back up. So cron is intended for systems which are
almost always on, such as servers.
Cron also allows regular users to create cron jobs. Each user has a personal
crontab file that can be accessed by running crontab -e. Of course
the script which is run can only do things the user can do. We can also control
who's allowed to create crontab files with the /etc/cron.allow and
/etc/cron.deny files.
Anacron
Anacron is a similar tool, but has a couple of differences. First, Anacron checks for tasks that were missed when the system boots up. So if we miss a task's time trigger when the machine is off, we will "make it up" when we boot. This means it can be a better choice when the machine is not assumed to run constantly. Also unlike cron, only root can create Anacron tasks.
Anacron tasks are recorded in the /etc/anacrontab file. Each
task contains:
- A period of days
- A delay in minutes
- A unique job identifier
- A shell command
For each job, Anacron checks to see if the task has been run in the amount of days specified. If not, it waits the minute delay for that task, and then runs it. It records a timestamp of the run for calculating when the job has to be run in the future.
1 5 backup /usr/local/bin/make-backup.sh 7 10 update-check apt list --upgradeable > /root/to-upgrade 30 60 reboot systemctl reboot
The minute delay is used to ensure that the jobs don't all run at once when the system boots up.