Crontab is a mechanism for launching tasks in the background.
Great for automatic backups, system heartbeat, and other periodic activities.
There are two ways to launch crontab tasks:
System-wide,
User-specific.
- User-specific - items listed in the special file shown using
crontab -l will be run as though invoked by the specific user.
Typically user-specific crontab files are stored in /var/spool/cron and
should only be changed using crontab -e.
$ crontab -l
00 * * * * /usr/bin/php /var/www/html/foobar.com/community/mail_digests.php
$ crontab -e
...
- System-wide - For system wide tasks a master crontab is
typically invoked by root user once a minute.
See /etc/crontab for details (don't even
THINK about editing this file). The cron.d folder is equivlent to
running crontab -l as root. Typically services (i.e. installable
packages) that use cron will use the system-wide form.
For manually based backups and
other highly custom/non-service/administration tasks the user-specific form
is a better choice - mostly because these tend to need frequent adjustments
and using crontab -e is cleaner than mucking about in /etc/cron... directly.
# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
# ls -ld /etc/cron.*
drwxr-xr-x 2 root root 4096 Feb 17 2004 cron.d
drwxr-xr-x 2 root root 4096 Mar 22 2005 cron.daily
drwxr-xr-x 2 root root 4096 Feb 15 2004 cron.hourly
drwxr-xr-x 2 root root 4096 Mar 21 2005 cron.monthly
drwxr-xr-x 2 root root 4096 Mar 21 2005 cron.weekly
# ls -lr cron.*
cron.weekly:
-rwxr-xr-x 1 root root 414 Oct 13 2003 makewhatis.cron
-rwxr-xr-x 1 root root 277 Feb 15 2004 0anacron
-rwxr-xr-x 1 root root 414 Nov 18 2004 00-makewhatis.cron
cron.monthly:
-rwxr-xr-x 1 root root 278 Feb 15 2004 0anacron
cron.hourly:
cron.daily:
-rwxr-xr-x 1 root root 136 May 11 2004 yum.cron
-rwxr-xr-x 1 root root 193 Feb 15 2004 tmpwatch
-rwxr-xr-x 1 root root 82 Apr 16 2004 slocate.cron
-rwxr-xr-x 1 root root 104 Dec 28 2004 rpm
-rwxr-xr-x 1 root root 1603 May 5 2004 prelink
-rwxr-xr-x 1 root root 418 Oct 13 2003 makewhatis.cron
-rwxr-xr-x 1 root root 180 Feb 15 2004 logrotate
-rwxr-xr-x 1 root root 276 Feb 15 2004 0anacron
-rwxr-xr-x 1 root root 135 Mar 27 2004 00webalizer
-rwxr-xr-x 1 root root 418 Nov 18 2004 00-makewhatis.cron
lrwxrwxrwx 1 root root 28 Mar 22 2005 00-logwatch -> ../log.d/scripts/logwatch.pl
cron.d:
-rw-r--r-- 1 root root 46 Aug 7 20:23 www-back
# cron.daily/rpm
#!/bin/sh
rpm -qa --qf '%{name}-%{version}-%{release}.%{arch}.rpm\n' 2>&1 \
| sort > /var/log/rpmpkgs
# cat /etc/cron.d/www-back
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
# MAILTO=root
HOME=/
# 10:05 PM daily, backup www
05 22 * * * root sh /public/archive/www-back.sh | mail -s "www backup" backups@foobar.com
Installing with yum ...
# yum list \*cron\*
Installed Packages
crontabs.noarch 1.10-8 installed
Available Packages
anacron.i386 2.3-45.el5.centos base
vixie-cron.i386 4:4.1-77.el5_4.1 base
yum-cron.noarch 0.6-1.el5.centos extras
# yum install vixie-cron
...
Installed:
vixie-cron.i386 4:4.1-77.el5_4.1
# crontab -l
no crontab for root
# service crond status
crond is stopped
# service crond start
Starting crond: [ OK ]
Don't forget to start crond after installing it.
Typically crond sends script output as email to user
accounts. You may need to check that sendmail
(or equivelent) and mailx are installed and running
for user accounts to recieve email.