Crontab计划任务
crontab简介
Crontab是一个用于设置周期性被执行的任务的工具
安装并检查Crontab
检查Crontab工具是否安装:
[root@senlong vhost]# crontab -l
检查crond服务是否启动:
[root@senlong vhost]# service crond status
crond (pid 1639) is running...
安装cron
yum install vixie-cron
yum install crontabs
示例
每分钟都打印当前时间到一个日志文件中
*/1 * * * * date >> /home/test/log.txt
Crontab的基本组成
Crontab的配置文件格式
30 21 * * * service httpd restart #每晚的21:30重启apache
45 4 1,10,22 * * service httpd restart # 每月1,10,22号的4:45重启apache
45 4 1-10 * * service httpd restart # 每月1到10号的4:45重启apache
*/2 * * * * service httpd restart #每隔两分钟重启apache
1-59/2 * * * * service httpd restart #分钟为奇数时重启apache
0 23-7/1 * * * service httpd restart #晚上11点到早上7点之间,每隔一小时重启apache
0,30 18-23 * * * service httpd restart #每天18:00--23:00之间每隔30分钟重启apache
0-59/30 18-23 * * * service httpd restart #每天18:00--23:00之间每隔30分钟重启apache
小结:
表示任何时候都匹配
“A,B,C”表示A或者B或者C时执行命令
“A-B”表示A到B之间时执行命令
“/A”表示每A分钟(小时等)执行一次命令
crontab工具的使用
[root@senlong test]# crontab -h
crontab: invalid option -- 'h'
crontab: usage error: unrecognized option
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)
Crontab配置文件
全局(系统)配置文件
可以在该文件中写入任务
[root@senlong test]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# 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
crontab的日志
/var/log/cron文件保存着cron执行的所有记录
[root@senlong log]# ll /var/log/cron*
-rw------- 1 root root 43262 Apr 30 17:50 /var/log/cron
-rw------- 1 root root 20090 Mar 7 09:43 /var/log/cron-20160307
-rw------- 1 root root 3432 Apr 4 10:20 /var/log/cron-20160404
-rw------- 1 root root 6168 Apr 20 11:21 /var/log/cron-20160420
-rw------- 1 root root 40228 Apr 24 10:44 /var/log/cron-20160424
常见错误
环境变量
[root@senlong ~]# vim .bash_profile #添加了环境变量 APPDIR=/etc export APPDIR
[root@senlong ~]# source .bash_profile
[root@senlong ~]# crontab -l
*/1 * * * * echo $APPDIR >> /tmp/appdir.log
# 计划任务识别不出该环境变量 $APPDIR
四月的第一个星期日早晨1时59分运行a.sh
59 1 1-7 4 0 /root/a.sh #错误,这样在1-7号与星期为0时都会执行,因为是或的关系
59 1 1-7 4 * test `date +\%w` -eq 0 && /root/a.sh # %前要加上\
[root@senlong ~]# date +%w
6
[root@senlong ~]# test `date +%w` -eq 0
[root@senlong ~]# echo $?
1
[root@senlong ~]# test `date +%w` -eq 6
[root@senlong ~]# echo $?
0
[root@senlong ~]# test `date +%w` -eq 0 && echo hello
[root@senlong ~]# test `date +%w` -eq 6 && echo hello
hello
命令行双引号中使用%时,要加上\
分钟设置误用
两个小时运行一次
* 0,2,4,8,10,12,14,16,18,20,22 * * * date #小时的罗列太繁琐,分钟设置为*表示每分钟都进行
0 */2 * * * date #在每小时的分钟为0时才运行
综合案例
Crontab最小只能设置每分钟执行一个命令,如果想每半分钟执行某个命令如何实现?
[root@senlong ~]# date;sleep 0.5s;date
[root@senlong ~]# crontab -l
*/1 * * * * date >> /tmp/date.log
*/1 * * * * sleep 30s; date >> /tmp/date.log #通过控制秒数实现精确到秒数级别的crontab
[root@senlong ~]# tail -f /tmp/date.log
Sat Apr 30 18:16:01 CST 2016
Sat Apr 30 18:16:31 CST 2016
Sat Apr 30 18:17:01 CST 2016
问题
测试:不同用户的计划任务能否执行
用户A的任务能否在用户B执行
root用户的任务在其他用户能否执行
订单扫描:
0 5 * * * php /mnt/bjwechat/yii securityscaner/start