cron
cron: 服务名称
crond: 后台进程
crontab: 定制好的任务计划表
配置文件
/etc/crontab
: 系统任务/var/spool/cron
下的文件: 用户任务
文件
说明
/etc/cron.deny
该文件中所列用户不允许使用crontab命令
/etc/cron.allow
该文件中所列用户允许使用crontab命令
/var/spool/cron/
or /var/spool/cron/crontabs
所有用户crontab文件存放的目录, 以用户名命名
查看用户定时任务
### 方法 1
bovenson@ThinkCentre:~$ crontab -l
no crontab for bovenson
### 方法 2
bovenson@ThinkCentre:~$ sudo cat /var/spool/cron/crontabs/bovenson
[sudo] bovenson 的密码:
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.uOvisi/crontab installed on Mon Sep 11 20:32:49 2017)
# (Cron version -- $Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $)
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
任务格式
* * * * * command
分钟(0-59) 小时(0-23) 日期(1-31) 月份(1-12) 星期(0-6,0代表星期天) 命令
第1列表示分钟1~59 每分钟用*或者 */1表示
第2列表示小时1~23(0表示0点)
第3列表示日期1~31
第4列表示月份1~12
第5列标识号星期0~6(0表示星期天)
第6列要运行的命令
在以上任何值中,星号(*)可以用来代表所有有效的值。譬如,月份值中的星号意味着在满足其它制约条件后每月都执行该命令。
整数间的短线(-)指定一个整数范围。譬如,1-4 意味着整数 1、2、3、4。
用逗号(,)隔开的一系列值指定一个列表。譬如,3, 4, 6, 8 标明这四个指定的整数
正斜线(/)可以用来指定间隔频率。在范围后加上 /\ 意味着在范围内可以跳过 integer。譬如,0-59/2 可以用来在分钟字段定义每两分钟。间隔频率值还可以和星号一起使用。例如,*/3 的值可以用在月份字段中表示每三个月运行一次任务
开头为井号(#)的行是注释,不会被处理
示例
0 1 * * * /home/testuser/test.sh
# 每天晚上1点调用/home/testuser/test.sh
*/10 * * * * /home/testuser/test.sh
# 每10钟调用一次/home/testuser/test.sh
30 21 * * * /usr/local/etc/rc.d/lighttpd restart
# 上面的例子表示每晚的21:30重启apache
45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart
# 上面的例子表示每月1、10、22日的4 : 45重启apache。
10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart
# 上面的例子表示每周六、周日的1 : 10重启apache。
0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart
# 上面的例子表示在每天18 : 00至23 : 00之间每隔30分钟重启apache
0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart
# 上面的例子表示每星期六的11 : 00 pm重启apache
* */1 * * * /usr/local/etc/rc.d/lighttpd restart
# 每一小时重启apache
* 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart
# 晚上11点到早上7点之间,每隔一小时重启apache
0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart
# 每月的4号与每周一到周三的11点重启apache
0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart
# 一月一号的4点重启apache
*/30 * * * * /usr/sbin/ntpdate 210.72.145.44
# 每半小时同步一下时间
配置任务
crontab [-u user] file
crontab [-u user] [-l| -r | -e][-i]
参数与说明:
crontab -u # 设定某个用户的cron服务
crontab -l # 列出某个用户cron服务的详细内容
crontab -r # 删除某个用户的cron服务
crontab -e # 编辑某个用户的cron服务
示例
### 使用命令行
$ crontab -e
# 进入编辑器; 编辑配置文件
### 从文件添加; 注意: 会清空原有任务
$ cat cron-task-conf-file.cron
* * * * * /home//home/public/Git/notes/Linux/Codes/cron-test-01.sh
$ crontab cron-task-conf-file.cron
$ crontab -l
* * * * * /home//home/public/Git/notes/Linux/Codes/cron-test-01.sh
### 从文件添加:指定用户
crontab [-u user] file
最后更新于
这有帮助吗?