Table of Contents

Linux

cron - How to use cron in Linux



/var/spool/cron/USER

This cron file can write by user.

Commands

CommandsNote
crontab -h Help
crontab -l
crontab -u USER -l
List
crontab -e
crontab -u USER -e
Edit
crontab -r
crontab -u USER -r
Remove

Format

1.minute  2.hour  3.dom  4.month 5.weekday   command

  1.  minute   Minute of the hour  0 th 59
  2.  hour     Hour of the day     0 to 23
  3.  dom      Day of the month    1 to 31
  4.  month    Month of the year   1 to 12
  5.  weekday  Day of the week     0 to 6 (0 = Sunday)

Example

0 * * *  * /home/shell/test.sh >/var/log/cron_root/test.log 2>&1
* * * *  * /home/shell/test.sh 1>/dev/null 2>/dev/null
0/10 * * *  * /home/shell/test.sh >/var/log/test.log 2>>/var/log/cron_root/test_error.log
0 12 * * * /home/shell/sample1.sh > /dev/null 2>&1        <- 12:00 Everyday
0 4 * * 0,1,2 /home/shell/sample2.sh  > /dev/null 2>&1       <-  4:00 Sunday, Monday, Tuesday
0 7 * * 1-5 /home/shell/sample3.sh  > /dev/null 2>&1   <- Monday - Friday 

Give the log a date
1 8,17 * * * * /home/shell/test1.sh >> /var/log/test-`date +\%Y\%m\%d-\%H\%M`.log 2>&1


/etc/cron.d/FILE

Format

# cat /etc/crontab

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

Example

/etc/cron.d/sysstat

# run system activity accounting tool every 10 minutes
*/10 * * * * root /usr/lib/sa/sa1 1 1
# generate a daily summary of process accounting at 23:53
53 23 * * * root /usr/lib/sa/sa2 -A


/etc/crontab

Format

# cat /etc/crontab

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed


/etc/cron.daily/


/etc/cron.weekly/


/etc/cron.hourly/


/etc/cron.monthly/