Show Lecture.Periodic as a slide show.
CT320 Periodic
⏰
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 don’t require human intervention, so
they are good candidates for scripts.
- Some tasks are inherently periodic, e.g.,
backups.
Daemons
$ ps -e | grep 'd$' | sort -k4 -u
1819 ? 00:00:00 atd
1075 ? 00:00:01 chronyd
1823 ? 00:00:04 crond
6559 ? 00:00:00 gvfsd
209 ? 00:00:00 kaluad
98 ? 00:00:00 kauditd
108 ? 00:00:00 kblockd
105 ? 00:00:02 khugepaged
100 ? 00:00:00 khungtaskd
107 ? 00:00:00 kintegrityd
208 ? 00:00:00 kmpath_rdacd
104 ? 00:00:00 ksmd
1172 ? 00:00:03 ksmtuned
2 ? 00:00:00 kthreadd
197 ? 00:00:00 kthrotld
139710 ? 00:00:00 kworker/0:1H-kblockd
2474416 ? 00:00:00 kworker/10:1H-kblockd
1420620 ? 00:00:00 kworker/11:3H-kblockd
189209 ? 00:00:00 kworker/1:8H-kblockd
108959 ? 00:00:00 kworker/2:8H-kblockd
165549 ? 00:00:00 kworker/3:0H-kblockd
20666 ? 00:00:00 kworker/4:0H-kblockd
2490624 ? 00:00:03 kworker/5:1H-kblockd
2880431 ? 00:00:00 kworker/6:2H-kblockd
3070522 ? 00:00:04 kworker/7:0H-kblockd
2252206 ? 00:00:11 kworker/8:1H-kblockd
431 ? 00:00:03 kworker/9:1H-kblockd
169312 ? 00:00:00 kworker/u24:0-rpciod
38716 ? 00:00:01 kworker/u24:1-rpciod
200098 ? 00:00:00 kworker/u24:2-xprtiod
121785 ? 00:00:00 kworker/u24:3-xprtiod
136261 ? 00:00:00 kworker/u24:4-nfsiod
1901 ? 00:00:00 lockd
1067 ? 00:00:00 lsmd
111 ? 00:00:00 md
1880 ? 00:00:00 nfsiod
1781 ? 00:00:30 pmcd
1079 ? 00:00:45 polkitd
14 ? 00:00:44 rcu_sched
1418 ? 00:00:00 rhsmcertd
1053 ? 00:00:00 rpcbind
1057 ? 00:00:00 rpciod
1417 ? 00:00:00 rpc.statd
1411 ? 00:00:25 rsyslogd
1408 ? 00:00:00 sshd
1 ? 00:03:10 systemd
1811 ? 00:00:45 systemd-logind
698 ? 00:00:12 systemd-udevd
1406 ? 00:05:27 tuned
1808 ? 00:00:06 /usr/sbin/httpd
114 ? 00:00:00 watchdogd
1058 ? 00:00:00 xprtiod
1177 ? 00:00:00 ypbind
- 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.
Permissions
$ cat /usr/lib/systemd/system/crond.service
[Unit]
Description=Command Scheduler
After=auditd.service nss-user-lookup.target systemd-user-sessions.service time-sync.target ypbind.service
[Service]
EnvironmentFile=/etc/sysconfig/crond
ExecStart=/usr/sbin/crond -n $CRONDARGS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=30s
[Install]
WantedBy=multi-user.target
$ ls -l /usr/sbin/crond
-rwxr-xr-x 1 root root 75712 Apr 6 05:40 /usr/sbin/crond
Is crond
SUID? How does it execute your crontab as you?
It’s executed as root
by systemd
.
Topics
- Overview
- Cron Daemon
- Crontab Format
- Crontab Command
- Common Uses
crontab Format
Comment lines starting with ‘#’ are ignored by the daemon.
Otherwise:
Label | Range | Description |
Minute | 0–59 | Minute of Hour |
Hour | 0–23 | Hour of Day |
Day | 1–31 | Day of Month |
Month | 1–12 | Month of Year (or “Jan”, “Feb”, …) |
Weekday | 0–6 | Day of Week (0=Sunday) (or “Sun”, “Mon”, …) |
crontab Schedules
# Minute, Hour, Day of Month, Month, Weekday
* * * * * echo Every minute
00 * * * * echo Every hour
00 1 * * * echo Every day at 1:00ᴀᴍ
30 */3 * * * echo Every three hours, on the half-hour
00 23 * * 0 echo 11:00ᴘᴍ Sundays
*/5 9-17 * * 1-5 echo Every five minutes, during working hours
45 10,22 * * 0,6 echo 10:45ᴀᴍ and 10:45ᴘᴍ on weekends
00 8 25 12 * echo Christmas morning
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 * * * *”.
What cron cannot do
cron
can’t help you with:
- 3rd Monday of the month
- Last Sunday of the month
- Last day of the month
- MLK day
- Election day
- Solstices
- Easter
- Ramadan
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
edits your crontab interactively
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
- The super-user 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)