回到顶部


一次性计划任务

命令: at
语法: at [-f 文件名] 时间
绝对计时方法 HH:MM yyyy-MM-dd
相对计时方法 now + n minutes now+n hours now + n days
描述: 安排一个或多个命令在指定的时间运行一次 (ctrl+d 保存任务退出)

周期性计划任务

Crontab是一个用于设置周期性被指定的任务的工具。

被周期性执行的任务,Cron Job
周期性执行的任务列表,我们称为 Cron Table
安装并检查CronTab服务

检查cron服务

-检查crontab工具是否安装: crontab -l
-检查crond服务是否启动:service crond status

安装cron

-yum install vixie-cron
-yum install crontabs
------------------------------------------------
[root@ssgao1987 ~]# crontab -l
no crontab for root
[root@ssgao1987 ~]# service crond status
crond (pid  1431) 正在运行...
简单实例
[root@ssgao1987 ~]# crontab -e (编辑任务)
[root@ssgao1987 ~]# crontab -l (显示任务列表)
*/1 * * * * date >> /tmp/date.txt
crontab的基本组成
CROND 系统服务
每分钟都会从配置文件刷新定时任务
文件方式,设置定时任务

crontab的配置文件格式

* * * * command
第一个* 分钟0~59
第二个* 小时0~23
第三个* 日期0~31
第四个* 月份1~12
第五个* 星期0~7
​
每晚的21:30分重启apache
30 21 * * * service httpd restart
 
每月1,10,22日的4:45重启apache
45 4 1,10,22 * * service httpd restart
​
每月1到10号的4:45重启apache
45 4 1-10 * * service httpd restart
​
每隔两分钟执行
*/2 * * * * service httpd restart
1-59/2 * * * *  service httpd restart
​
晚上11点到早上7点之间,每隔一小时重启apache
0 23-7/1 * * * service httpd restart
​
每天18:00到23:00之间每隔30分钟重启apache
0,30 18-23 * * *  service httpd restart
0-59/30 18-23 * * *  service httpd restart

小结

* 表示任何时候都匹配
可以用"A,B,C"表示A或者B或者C时执行命令
可以用"A-B"表示A到B之间时执行命令
可以用"*/A"表示每A分钟(小时等)执行一次命令
crontab工具的使用
[root@ssgao1987 ~]# crontab -help
usage:  crontab [-u user] file  可以查看某个用户的crontab
    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 -e 其实就是修改的文件 /var/spool/cron/root 
crontab的配置文件
全局(系统)配置文件
/etc/crontab

root配置文件

只有root用户才会把这些内容载入到计划任务列表中
[root@ssgao1987 etc]# cat 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
....'这里添加计划任务'.....
*/1 * * * * date >> date.log

crontab的日志

/var/log/cron 文件保存着cron的任务执行记录
posted on 2018-04-18 06:13  ssgao  阅读(134)  评论(0编辑  收藏  举报