Useful tips for crontab

1 minute read

Cron service can be used to schedule obvious things, such as regular backups that occur daily or weekly. The crond daemon is the background service that enables cron functionality. The cron service checks for files in the /var/spool/cron and /etc/cron.d directories and the /etc/anacrontab file.

Crontab (cron table) is a text file that specifies the schedule of cron jobs. There are two types of crontab files. The system-wide crontab files and individual user crontab files.

  • /etc/cron.d : system-wide crontab files
  • /var/spool/cron : individual user crontab files

Users crontab files are stored by the user’s name, and their location varies by operating systems. Crontab files are stored in the following directory.

  • /var/spool/cron : Red Hat based system such as CentOS
  • /var/spool/cron/crontabs : Debian and Ubuntu

/etc/crontab and the files inside the /etc/cron.d directory are system-wide crontab files that can be edited only by the system administrators.

Show list : crontab -l

(1) minute       : 0 - 59 
(2) hour         : 0 - 23 
(3) day_of_month : 1 - 31 
(4) month        : 1 - 12 
(5) weekday      : 0 - 6 (0:Sunday, 1:Monday, ..., 6:Saturday)
(6) command

Edit crontab : crontab -e

* * * * * /usr/local/bin/test.sh >> /dev/null
-> Every minute

0 * * * * echo The hour is `date`. > /dev/console
-> Every hour

00 06 * * 0-6 /usr/local/bin/test.sh >> /dev/null
-> From Sunday to Saturday at 6:00 a.m

0 */3 * * * /usr/local/bin/test.sh >> /dev/null
-> Every 3 hours (Need to change minute parameter to 0.)

10,20,30,40,50,60 * * * * /usr/local/bin/test.sh >> /logs/test.log
-> 10,20,30,50,60 minutes every hour

30 17 * * 5 /usr/bin/banner "Time To Go!" > dev/console 
-> Every Friday at 17:30

30 6 * * 1,3,5 /usr/bin/calendar
-> Sunday, Wednesday, Friday at 6:30

30 6 * * * /usr/bin/calendar
-> Every day at 6:30

0 0 * 8 * /u/harry/bin/maintenance
-> August only at 00:00

0 16 * 12 5 /usr/sbin/wall%HAPPY HOLIDAY!%Remember to turn in your time card.
-> Every Saturday in December, 4:00 pm

Monitor crontab log

sudo tail -f /var/log/syslog | grep CRON

Tags:

Categories:

Updated:

Leave a comment