参考文献

crontab

  • crondlinux下的一个守护进程,用于定期执行某些任务或等待某些事件被处理,类似于windows下的计划任务,当操作系统安装完成后,会默认安装这个服务工具,并会自动启动crond进程,crond进程每分钟会定期检查是否有任务需要执行,如果有任务就会自动执行.如果有任务要执行,就会自动执行. /etc目录下有一个crontab文件,它是系统任务调度的配置文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    # /etc/crontab: system-wide crontab
    # Unlike any other crontab you don't have to run the `crontab'
    # command to install the new version when you edit this file
    # and files in /etc/cron.d. These files also have username fields,
    # that none of the other crontabs do.

    SHELL=/bin/sh
    PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

    # 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
    • 星号(*):代表所有可能的值,例如月份字段为星号,则表示满足其他字段的约束后,每月执行该命令操作
    • 逗号 (,):可以指定以逗号分隔的值的列表范围,例如“1,2,5,7,8,9”
    • 中线(-):可以在整数之间使用中线来指示整数范围,例如“2-6”表示“2,3,4,5,6”
    • 斜杠(/):可以使用斜杠指定时间间隔的频率,例如“0-23/2”表示每两小时一次.正斜杠也可以与星号一起使用,例如*/10,如果用在分钟字段中,则表示每十分钟一次.

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#每天上午8点运行一次脚本:
0 8 * * * /path/to/script.sh

#每周一下午2点运行一次备份脚本:
0 14 * * 1 /path/to/backup.sh

#每隔10分钟执行一次任务:
*/10 * * * * /path/to/task.sh

#每月的1号凌晨3点运行一次清理脚本:
0 3 1 * * /path/to/cleanup.sh

#每天的上午9点到下午6点之间,每隔30分钟执行一次任务:
*/30 9-18 * * * /path/to/task.sh