Saturday, May 14, 2011

A daemon for your every command: cron

Cron is an excellent way to do regular tasks.  It uses 'vi' as its editor, and the only real requirement is that you use full path names like:
/usr/sbin/portsnap
Instead of:
portsnap
From the root user type:
# crontab -e
This will bring up the schedule to be edited.  The schedule follows the columns of minutes, hours, days, months and day of the week and is very customizable.  FreeBSD allows the switches '@daily', '@weekly', etc, not all systems will allow those.  All of the following examples are single lines.

For example, to schedule the download and update of the new ports tree on a FreeBSD system at 4:41am every day, add:
41 4 * * * /usr/sbin/portsnap -I cron &&/usr/sbin/portsnap update
If you wanted to update the ports you set up with ezjail at 5:05am, add:
5 5 * * * /usr/local/bin/ezjail-admin update -P
To zip and archive your system config files at midnight add this on a single line:

@daily /usr/bin/tar zcvf /tank/backup/etc.tar.gz /etc&&/usr/bin/tar zcvf /tank/backup/usrlocaletc.tar.gz /usr/local/etc
Back up your mysql database every night at 3:00am, add:
0 3 * * * /usr/local/bin/mysqldump -hlocalhost -uintranetguy -pd0ntTe3ll intranet > /var/sqlbackup/intranet.sql
And don't forget your weekly scrub for your ZFS array:
@weekly /sbin/zpool scrub tank
Just like 'vi' you can Esc to command mode and exit with a ':' followed by 'wq' and Enter.

No comments:

Post a Comment