at
在指定时刻执行程序,输入 Ctrl + D 退出 at
命令
at 22:10
at 22:10 tomorrow
at 22:10 12/10/19
在 2019 年 12 月 10 日 22 点 10 分执行
at now +10 minutes
在 10 分钟之后执行
[yumanli@punk ~]$ at 22:10 tomorrow
at> touch file.txt
at> <EOT>
job 1 at Tue Oct 13 22:10:00 2020
atq
列出正在等待执行的 at 任务
[yumanli@punk ~]$ atq
1 Tue Oct 13 22:10:00 2020 a yumanli
atrm
删除正在等待执行的 at 任务
atrm 编号
[yumanli@punk ~]$ atrm 1
[yumanli@punk ~]$ atq
[yumanli@punk ~]$
sleep
暂停一段时间
sleep 5
暂停 5 秒
sleep 5m
暂停 5 分钟
sleep 5h
暂停 5 小时
sleep 5d
暂停 5 天
命令分隔符
可以用 ;
隔开多条命令,在一行输入多条命令。一条执行完执行下一条,不管上一条命令是否执行成功,都会执行下一条命令
[yumanli@punk ~]$ touch file.txt;sleep 15m;rm -rf file.txt
用 &&
隔开多条命令,前一条执行成功,才执行下一条
用 ||
隔开多条命令,前一条执行失败,才执行下一条
crontab
定期执行程序,可以重复执行
crontab
是一个命令,用来读取和修改名为 crontab 的文件,crontab 文件包含了你要定时执行的程序列表,也包含了执行的时刻。
实际上有两个命令,crontab
用来修改 crontab 文件,cron
用来实际执行定时的程序
crontab -l
显示 crontab 文件
crontab -e
修改 crontab 文件
crontab -r
删除 crontab 文件
crontab 文件的格式:m h dom mon dow command
,*
代表该位置不填写
- m: 分钟 0~59
- h: 小时 0~23
- dom: 一个月的哪一天 1~31
- mon: 月份 1~12
- dow:星期几 0~6 (星期天是 0)
- command: 需要定时执行的命令
# 每天 22:10 分都在家目录下创建 file.txt 文件
10 22 * * * touch ~/file.txt
# 每礼拜一、三、四凌晨都执行 command 命令
0 0 * * 1,3,4 command
# 每个月的 1~15 日的 5: 30 都执行 command 命令
30 5 1-15 * * command
# 每 2 个小时的整点 (0点,2点,4点,6点等等) 都执行 command 命令
0 */2 * * * command
# 每礼拜一到礼拜五的每个 10 的倍数的分钟(10,20,30 等等),都执行 command 命令
*/10 * * * 1-5 command
网友评论