Tutorial 09 · FreeBSD

cron and periodic

Schedule your own jobs with cron and use periodic for the system's daily, weekly and monthly housekeeping.

Last updated

cron runs scheduled commands. FreeBSD also ships periodic, a framework of maintenance scripts that cron runs daily, weekly and monthly, emailing a report to root.

1Per-user crontabs

crontab -e     # edit your crontab
crontab -l     # list it

Fields are minute, hour, day of month, month and day of week, then the command. Cron uses a minimal environment, so use full paths.

# m  h  dom mon dow  command
15  2  *   *   *    /usr/local/bin/rsync -a /home/alice/docs/ /backup/docs/
*/10 * *   *   *    /home/alice/bin/check.sh >/dev/null 2>&1
@reboot             /home/alice/bin/on-boot.sh

2System crontab

/etc/crontab has an extra user field. Rather than editing it, packages and admins can drop files into /usr/local/etc/cron.d/ using the same format.

# /usr/local/etc/cron.d/zfs-snap
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
0 * * * * root zfs snapshot -r zroot/data@hourly-$(date +\%Y\%m\%d\%H)

In crontab files, % has a special meaning and must be escaped as \%.

3periodic

The scripts live in /etc/periodic/{daily,weekly,monthly,security} and /usr/local/etc/periodic/. Defaults are in /etc/defaults/periodic.conf; override them in /etc/periodic.conf.

sysrc -f /etc/periodic.conf daily_scrub_zfs_enable=YES
sysrc -f /etc/periodic.conf daily_status_zfs_enable=YES
sysrc -f /etc/periodic.conf daily_output=/var/log/daily.log   # log instead of mailing
periodic daily     # run it now to test

4Reading the reports

By default reports are mailed to root locally. Read them with mail, or forward root's mail by setting root: in /etc/mail/aliases (then run newaliases), or log to files as above.

← All FreeBSD tutorials