Helpful crontab commands and scripts.
Install Cronie on Arch Linux:
pacman -S cronie
Set nano as your editor to edit crontab:
export EDITOR="nano"
CronTab Examples
| Time & Date | Description |
|---|---|
| */1 * * * * | run every minute |
| */5 * * * * | run every 5 minutes |
| */15 * * * * | run every 15 minutes |
| */30 * * * * | run every 30 minutes |
| * */1 * * * | run every hour |
| * */5 * * * | run every 5 hours |
| * */12 * * * | run every 12 hours |
| * * */1 * * | run every day |
| * * */3 * * | run every 3 days |
| * * */7 * * | run every 1 week |
| * * */14 * * | run every 2 weeks |
| * * */21 * * | run every 3 weeks |
| * * * */1 * | run every month |
| * * */15 */1 * | run every month and a half |
| * * * */3 * | run every 3 months |
| * * * */6 * | run every 6 months |
| * * * */12 * | run every year |
| * * */6 */12 * | run every year and a half |
| * * * */24 * | run every 2 years |
| * * * */60 * | run every 5 years |
Note: This are just few examples of time and date usage that is possible in cron.
CronTab Alias
| Alias | Description |
|---|---|
| @reboot | Run once, at startup. |
| @yearly | Run once a year, “0 0 1 1 *”. |
| @annually | (sames as @yearly) |
| @monthly | Run once a month, “0 0 1 * *”. |
| @weekly | Run once a week, “0 0 * * 0”. |
| @daily | Run once a day, “0 0 * * *”. |
| @midnight | (same as @daily) |
| @hourly | Run once an hour, “0 * * * *”. |
Date Alias
date +"%Y-%m-%d"
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
%% a literal % %a locale's abbreviated weekday name (e.g., Sun) %A locale's full weekday name (e.g., Sunday) %b locale's abbreviated month name (e.g., Jan) %B locale's full month name (e.g., January) %c locale's date and time (e.g., Thu Mar 3 23:05:25 2005) %C century; like %Y, except omit last two digits (e.g., 20) %d day of month (e.g., 01) %D date; same as %m/%d/%y %e day of month, space padded; same as %_d %F full date; same as %Y-%m-%d %g last two digits of year of ISO week number (see %G) %G year of ISO week number (see %V); normally useful only with %V %h same as %b %H hour (00..23) %I hour (01..12) %j day of year (001..366) %k hour, space padded ( 0..23); same as %_H %l hour, space padded ( 1..12); same as %_I %m month (01..12) %M minute (00..59) %n a newline %N nanoseconds (000000000..999999999) %p locale's equivalent of either AM or PM; blank if not known %P like %p, but lower case %r locale's 12-hour clock time (e.g., 11:11:04 PM) %R 24-hour hour and minute; same as %H:%M %s seconds since 1970-01-01 00:00:00 UTC %S second (00..60) %t a tab %T time; same as %H:%M:%S %u day of week (1..7); 1 is Monday %U week number of year, with Sunday as first day of week (00..53) %V ISO week number, with Monday as first day of week (01..53) %w day of week (0..6); 0 is Sunday %W week number of year, with Monday as first day of week (00..53) %x locale's date representation (e.g., 12/31/99) %X locale's time representation (e.g., 23:13:48) %y last two digits of year (00..99) %Y year %z +hhmm numeric time zone (e.g., -0400) %:z +hh:mm numeric time zone (e.g., -04:00) %::z +hh:mm:ss numeric time zone (e.g., -04:00:00) %:::z numeric time zone with : to necessary precision (e.g., -04, +05:30) %Z alphabetic time zone abbreviation (e.g., EDT) |
CronTab Backup Script
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# # crontab # ## minute (0-59), hour (0-23, 0 = midnight), day (1-31), ## month (1-12), weekday (0-6, 0 = Sunday), command ## variables DAY = `date +"%F"` ## Date Format [2012-02-14] BAK = '~/backups' ## /home/$USER/backups SRC = '~/' ## /home/$USER/ ## regular backups @daily tar czf $BAK/user_home_backup_$DAY.tgz $SRC/* |
Note: final filename will be “user_home_backup_2012-02-14.tgz”.