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
|