See this page as a slide show
CT 320: Network and System Administration
CHAPTER 9: PERIODIC PROCESSES
Original slides from Dr. James Walden at Northern Kentucky University.
Topics
- Overview
- Cron Daemon
- Crontab Format
- Crontab Command
- Common Uses
Overview
- Why should a sysadmin repeatedly type long
sequences of commands to do repetitive tasks?
- Scripting and automation provide consistency and
reliability, and prevent typing errors.
- Shell, Perl, or Python scripts can incorporate lengthy
tasks and encode system expertise.
- Some tasks do not require human intervention, thus
they are good candidates for scripting.
- Some tasks are inherently periodic, e.g. regularly
scheduled backup of files and directories.
Daemons
- A daemon is a computer program that runs as a
background process.
- Daemons are programs that do not require user
intervention to perform their work.
- Daemons in Linux typically have names that end in the letter ‘d’:
syslogd
: system logging daemon
sshd
: daemon to handle incoming ssh connections
nfsd
: network file system daemon
ypbind
: network information service server
crond
: task scheduler daemon
Topics
- Overview
- Cron Daemon
- Crontab Format
- Crontab Command
- Common Uses
cron Daemon
- The cron daemon is the standard tools for running
periodic commands or scripts.
- The cron daemon reads configuration files with list
of commands and their associated schedules.
- The command lines are executed by default with sh,
but can also use other shells.
- The cron configuration file is called a crontab,
stored in
/var/spool/cron/
username
- Commands run using the UID and GID of the user associated with a crontab.
Topics
- Overview
- Cron Daemon
- Crontab Format
- Crontab Command
- Common Uses
crontab Format
- Comment lines starting with ‘#’ are ignored by the daemon.
- Each line that is not a comment has six fields:
minute hour dom month weekday command
- The first five fields tell cron when to run:
- minute (Minute of Hour) 0..59
- hour (Hour of Day) 0..23
- dom (Day of Month) 1..31
- month (Month of Year) 1..12
- weekday (Day of Week) 0..6, 0 = Sunday, 1 = Monday, …
crontab Schedules
* * * * * * echo Every minute
* 0 * * * * echo Every hour
* 0 1 * * * echo Every day at 1:00㏂
* 0 23 * * 1-5 echo 11:00㏘, Monday through Friday
* 45 10,22 * * 0,6 echo 10:45㏂ and 10:45㏘ on weekends
crontab shortcuts
@reboot : Run once after reboot.
@yearly : Run once a year, i.e., “0 0 1 1 *”.
@annually : Run once a year, i.e., “0 0 1 1 *”.
@monthly : Run once a month, i.e., “0 0 1 * *”.
@weekly : Run once a week, i.e., “0 0 * * 0”.
@daily : Run once a day, i.e., “0 0 * * *”.
@hourly : Run once an hour, i.e., “0 * * * *”.
crontab Example
PATH=/usr/local/bin:/home/bonehead/bin:/bin:/usr/bin
MAILTO=Bonehead@ColoState.Edu
0 2 1-10 * * du -h -c -d=1 /
- Changes environment variables for script
- Shell initialization (
.chsrc
, .bashrc
) are not processed
- Can mail output to user upon completion
- At 2:00㏂ on the 1st through 10th of the month,
run a disk usage command (could be a script)
Topics
- Overview
- Cron Daemon
- Crontab Format
- Crontab Command
- Common Uses
crontab Command
crontab
filename installs filename as crontab
crontab -e
checks out crontab and starts editor
crontab -l
lists the current crontab file
crontab -l >~/.crontab-backup
protects you from system reconfiguration.
crontab -r
removes the crontab file
- A file exists for each user, including root
- Root can supply a user, e.g.
crontab -r smith
/etc/cron.allow
and /etc/cron.deny
specify which
users can submit crontab files
Topics
- Overview
- Cron Daemon
- Crontab Format
- Crontab Command
- Common Uses
Common Uses
- Simple reminder:
30 4 25 * * mail -s "TPS" bing%End-of-month reports due
- Filesystem Backup
- Filesystem Cleanup
- Network Distribution
- Logfile Management
- Time Synchronization (or use service instead)