Crontabs Guide

Introduction

1
man crontab

—>

Crontab is the program used to install, remove or list the tables used to drive the cron(8) daemon. Each user can have their own crontab, and though these are files in /var/spool/ , they are not intended to be edited directly. For SELinux in mls mode can be even more crontabs - for each range. For more see selinux(8).

The cron jobs could be allow or disallow for different users. For classical crontab there exists cron.allow and cron.deny files. If cron.allow file exists, then you must be listed therein in order to be allowed to use this command. If the cron.allow file does not exist but the cron.deny file does exist, then you must not be listed in the cron.deny file in order to use this command. If neither of these files exists, only the super user will be allowed to use this com-mand. The second option is using PAM authentication, where you set up users, which could or couldn’t use crontab and also system cron jobs from /etc/cron.d/.

The temporary directory could be set in enviroment variables. If it’s not set by user than /tmp is used.

Installation

Just the commands:

1
2
3
4
5
6
7
8
9
10
11
12
13
# check installation
rpm -ql crontabs
rpm -qa crontabs
# install
yum install crontabs
# start service on boot
chkconfig crond on
# start service now
service crond start
service crond status
service crond restart
service crond reload
service crond stop

Also, you can check the service by:

1
ntsysv

Usage

Basic Usage

Also, man is our best friend :

1
man crontab

  • NAME
    crontab - maintain crontab files for individual users
  • SYNOPSIS
    crontab [-u user] file
    crontab [-u user] [-l | -r | -e] [-i] [-s]
  • OPTIONS
    **-u**     Append the name of the user whose crontab is to be tweaked.  If this option is not given, crontab examines "your" crontab, i.e., the crontab of the  person  executing  the  command.   Note  that  su(8) can confuse crontab and that if you are running inside of su(8) you should always use the -u option for safety’s sake.  The first form of this command is used to install a new crontab from some named file or standard input  if  the  pseudo-filename  "-"  is given.
    **-l**     The current crontab will be displayed on standard output.
    **-r**     The current crontab will be removed.
    **-e**     This  option  is used to edit the current crontab using the editor specified by the VISUAL or EDITOR environment variables.  After you exit from the editor, the modified crontab will be installed automatically.
    **-i**     This option modifies the -r option to prompt the user for a ’y/Y’ response before actually removing the crontab.
    **-s**     It will append the current SELinux security context string as an MLS_LEVEL setting to the crontab file before editing / replacement occurs - see the doc-umentation of MLS_LEVEL in crontab(5).
    
  • FILES
    /etc/cron.allow
    /etc/cron.deny
    

Shortly for this :
>

  • usage:
    crontab [-u user] file
    crontab [-u user] [ -e | -l | -r ]
    (default operation is replace, per 1003.2)
    
    -e (edit user’s crontab)
    -l (list user’s crontab)
    -r (delete user’s crontab)
    -i (prompt before deleting user’s crontab)
    -s (selinux context)

The format of -e command is:

1
2
3
4
5
6
7
8
9
  *    *    *    *    *   command
minus hour day month week command

minus: 1~59 minus, * or */1 for per minus
hour: 1~23, 0 means 0 o'clock
day: 1~31
month: 1~12
week: 0~6, 0 means sunday
command: the command u want to run

Examples

1
2
3
# every night at 21:31 reload nginx
# 每晚的21:30
30 21 * * * /usr/bin/nginx -s reload
1
2
3
# 1, 10, 22 per month at 4:45 reload nginx
# 每月1、10、22日的4:45
45 4 1,10,22 * * /usr/bin/nginx -s reload
1
2
3
# saturday and sunday per week at 1:10 reload nginx
# 每周六、周日的1:10
10 1 * * 6,0 /usr/bin/nginx -s reload
1
2
3
# 18:00 ~ 23:00 per 30 minus everyday reload nginx
# 每天18:00至23:00之间每隔30分钟
0,30 18-23 * * * /usr/bin/nginx -s reload
1
2
3
# saturday per week at 23:00 reload nginx
# 每星期六的23:00
0 23 * * 6 /usr/bin/nginx -s reload
1
2
3
# 23:00 ~ 7:00 per hour
# 23点到7点之间,每隔一小时
0 23-7/1 * * * /usr/bin/nginx -s reload
1
2
3
# per hour
# 每一小时
* */1 * * * /usr/bin/nginx -s reload
1
2
3
# 4 every month and monday to wednesday every week at 11:00
# 每月的4号与每周一到周三的11点
0 11 4 * mon-wed /usr/bin/nginx -s reload
1
2
3
# 1 january at 4:00
# 一月一号的4点
0 4 1 jan * /usr/bin/nginx -s reload
1
2
3
# per 30 minus
# 每半小时
*/30 * * * * /usr/bin/nginx -s reload

Advanced Usage

For the files:

  • /etc/cron.allow : list the user who can use crontab
  • /etc/cron.deny : list the user who cannot use crontab
  • /var/spool/cron : all users and their crontab will be store here
  • /var/log/cron : cron log per job
  • /etc/crontab : for system jobs, crontab -e is for current user
  • /etc/cron.d/* : seperated cron file
1
2
3
4
5
6
7
8
9
ls /etc/cron.d
0hourly raid-check

cat /etc/cron.d/0hourly
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
01 * * * * root run-parts /etc/cron.hourly
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
ls /etc/cron.hourly/
0anacron

cat /etc/cron.hourly/0anacron
#!/bin/bash
# Skip excecution unless the date has changed from the previous run
if test -r /var/spool/anacron/cron.daily; then
day=`cat /var/spool/anacron/cron.daily`
fi
if [ `date +%Y%m%d` = "$day" ]; then
exit 0;
fi

# Skip excecution unless AC powered
if test -x /usr/bin/on_ac_power; then
/usr/bin/on_ac_power &> /dev/null
if test $? -eq 1; then
exit 0
fi
fi
/usr/sbin/anacron -s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cat /etc/anacrontab 
# /etc/anacrontab: configuration file for anacron

# See anacron(8) and anacrontab(5) for details.

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22

#period in days delay in minutes job-identifier command
1 5 cron.daily nice run-parts /etc/cron.daily
7 25 cron.weekly nice run-parts /etc/cron.weekly
@monthly 45 cron.monthly nice run-parts /etc/cron.monthly

More Info.

Practice

I want to backup my svn daily, so i must have a script to backup:

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash

cd /home/svn/repos
now=`/bin/date +%Y%m%d`
/bin/tar czvf "kk_backup_$now.tar.gz" kk/ && rm -rf /home/svn/backup/* && /bin/mv kk_backup_*.tar.gz /home/svn/backup/
if [ $? == 0 ]
then
result="backup svn OK!!"
else
result="backup svn False!!"
fi
echo $result

then just give the script to /etc/cron.daily:

1
2
cp scripts/backup.sh /etc/cron.daily/svn_backup.sh
chmod +x /etc/cron.daily/svn_backup.sh

That’s all.

REF

鳥哥的 Linux 私房菜 – 第十五章、例行性工作排程(crontab)
centos中crontab(计时器)用法详解 - 长城的草 - 博客园