一.定时任务at
(1)添加定时任务
[root@localhost mnt]# at 21:50
at> rm -fr /mnt/*
at> <EOT>
job 1 at Thu Aug 31 21:50:00 2017 #ctrl+d
Can't open /var/run/atd.pid to signal atd. No atd running?
(2)查看定时任务
[root@localhost mnt]# at -l
1 Thu Aug 31 21:50:00 2017 a root
(3)取消定时任务
[root@localhost mnt]# atrm 1
[root@localhost mnt]# at -l
二.循环任务
crond 服务
sudo service crond start
简单例子
创建、编辑和维护定时任务
crontab -e # 此时打开vim界面 , 编辑本用户的crontab文件
#比如vim中添加 :
# 分 时 天 周 月 命令
*/1 * * * * echo `date`>>/home/user/test.txt
1
2
3
4
1
2
3
4
查看定时任务
user@linux-msx4:~> crontab -l
DO NOT EDIT THIS FILE - edit the master and reinstall.
(/tmp/crontab.B7p3lx installed on Thu Nov 12 14:01:14 2015)
(Cronie version 4.2)
*/1 * * * * echo `date`>>/home/user/test.txt
1
2
3
4
5
1
2
3
4
5
删除所有crontab定时任务
user@linux-msx4:~> crontab -r
user@linux-msx4:~> crontab -l
no crontab for user
user@linux-msx4:~>
1
2
3
4
1
2
3
4
编辑 /etc/crontab 维护定时任务
格式比crontab在command之前多了username :
*/1 * * * * user echo `date`>>/home/user/test.txt
1
1
常用命令参数
-u 指定一个用户
-l 列出某个用户的任务计划
-r 删除某个用户的任务
-e 编辑某个用户的任务
编辑命令格式
crontab -e
> Minute Hour Day Month Dayofweek command
分钟 小时 天 月 天每星期 命令
1
2
1
2
vim /etc/crontab
Minute Hour Day Month Dayofweek Username command
分钟 小时 天 月 天每星期 用户 命令
1
2
1
2
符号
“*”代表取值范围内的数字,
“/”代表”每”,
“-”代表从某个数字到某个数字,
“,”分开几个离散的数字
例子
/5 * * * * ls
5 * * * * ls # 指定每小时的第5分钟执行一次ls命令
30 5 * * * ls # 指定每天的 5:30 执行ls命令
30 7 8 * * ls # 指定每月8号的7:30分执行ls命令
30 5 8 6 * ls # 指定每年的6月8日5:30执行ls命令
30 6 * * 0 ls # 指定每星期日的6:30执行ls命令
[注:0表示星期天,1表示星期1,