16. Cron & At
1. Cron & at provides a way to schedule tasks.
o vixie-cron – Provides crond daemon and crontab editing utilities.
o crontabs – Provides default root crontab files.
o at – Provides atd daemon and command line utilities.
16.2 Crontab Files
User or system defined files that contain a command to execute and the time to execute it. Crond wakes every minute to see if any crontab files have changed and re-reads them if they have.
1. User crontabs
o Stored as /var/spool/cron/
o View with: crontab -l
o Edit with: crontab -e
o Edit a specific users crontab(root only): crontab -u
2. System crontabs
Crontab file that configures when scripts in cron.hourly, cron.daily, cron.weekly, and cron.monthly are executed.
This directory contains actual crontab files that are configured just like user crontab files.
Scripts in this directory are executed on the first minute of every new hour.
Scripts in this directory are executed at 4:02 AM every day.
Scripts in this directory are executed at 4:22 AM every Sunday.
Scripts in this directory are executed at 4:42 AM on the first day of the month.
3. Crontab Format
Minute – 0-59
Hour – 0-23
Day of Month – 1-31
Month – 1-12 *or*
– Jan, Feb, Apr, etc.
Day of Week – 0-7 (0 or 7 = Sunday) *or*
– Sun, Mon, Tue, Wed, Thu, Fri, Sat
Can specify comma separate lists and ranges for each parameter but only in a numeric format (e.g. 1-5 is ok for day of week, but not Mon-Fri).
# To execute foo every 5 minutes.
0,5,10,15,20,25,30,35,40,45,50,55 * * * * foo
# – OR –
*/5 * * * * foo
# Executes bar during the bottom of every hour
# during working hours on week days.
30 8-5 * * 1-5 bar
16.3 At Jobs
1. “at” jobs are configured from the command prompt. No crontab style files.
2. At uses the existing environment that the “at” command was executed in to run the specified command(s) at the indicated time. This typically makes at jobs easier/quicker to setup than crontab jobs because the environment is already configured for the job.
4. at 8:00 am March 12 # Execute specified commands at 8:00 am on March 12th
5. at now +3 hours # Execute specified commands 3 hours from now
6. at 9:30 pm -f ~/cmds # Execute commands in the ~/cmds file at 9:30 pm
After specifying a time, the user is prompted for the commands to execute unless the “-f” option is used to specify a file containing the commands to execute.
7. Managing At Jobs
o atq – List pending jobs for a user.
o atrm – Delete pending jobs.
o atrm 1 # Remove job number 1 from pending queue
o batch – Execute specified command when system load levels are low enough to permit it.
16.4 Access Control
If it exists, a user must be listed in this file in order to use crontab.
If it exists, a user must not be listed in this file in order to user crontab.
Same as cron.allow, only for “at”.
Same as cron.deny, only for “at”.
16. Cron & At