yxx554243496的个人博客分享 http://blog.sciencenet.cn/u/yxx554243496

博文

[转载]Linux -at 之定时提交任务

已有 2040 次阅读 2019-1-9 16:19 |个人分类:Linux维护|系统分类:科研笔记| Linux, 定时提交任务 |文章来源:转载

博文转自https://segmentfault.com/a/1190000007916299

1、at

命令at从文件或标准输入中读取命令并在将来的一个时间执行,只执行一次。at的正常执行需要有守护进程atd(关于systemctl请看这一篇):

#安装atyum install -y at 或 apt-get install at -y#启动守护进程service atd start 或 systemctl start atd#查看是否开机启动chkconfig --list|grep atd 或 systemctl list-unit-files|grep atd#设置开机启动chkconfig --level 235 atd on 或 systemctl enable atd

如果不使用管道|或指定选项-f的话,at的执行将会是交互式的,需要在at的提示符下输入命令:

[root@centos7 temp]# at now +2 minutes #执行at并指定执行时刻为现在时间的后两分钟at> echo hello world > /root/temp/file #手动输入命令并回车at> <EOT>                              #ctrl+d 结束输入job 9 at Thu Dec 22 14:05:00 2016      #显示任务号及执行时间[root@centos7 temp]#

选项-l或命令atq查询任务

[root@centos7 temp]# atq9       Thu Dec 22 14:05:00 2016 a root

到达时间后任务被执行,生成一个新文件file并保存echo的输出内容

[root@centos7 temp]# ls -l file -rw-r--r-- 1 root root 12 12月 22 14:05 file
[root@centos7 temp]# cat file hello world
[root@centos7 temp]#

at指定时间的方法很丰富,可以是
1)hh:mm小时:分钟(当天,如果时间已过,则在第二天执行)
2)midnight(深夜),noon(中午),teatime(下午茶时间,下午4点),today,tomorrow
3)12小时计时制,时间后加am(上午)或pm(下午)
4)指定具体执行日期mm/dd/yy(月/日/年)或dd.mm.yy(日.月.年)
5)相对计时法now + n units,now是现在时刻,n为数字,units是单位(minutes、hours、days、weeks)
如明天下午2点20分执行创建一个目录

[root@centos7 temp]# at 02:20pm tomorrowat> mkdir /root/temp/X
at> <EOT>
job 11 at Fri Dec 23 14:20:00 2016

选项-d或命令atrm表示删除任务

[root@centos7 temp]# at -d 11 #删除11号任务(上例)[root@centos7 temp]# atq[root@centos7 temp]#

可以使用管道|或选项-fat从标准输入或文件中获得任务

[root@centos7 temp]# cat test.txt echo hello world > /root/temp/file
[root@centos7 temp]# at -f test.txt 5pm +2 daysjob 12 at Sat Dec 24 17:00:00 2016[root@centos7 temp]# cat test.txt|at 16:20 12/23/16job 13 at Fri Dec 23 16:20:00 2016

atd通过两个文件/etc/at.allow/etc/at.deny来决定系统中哪些用户可以使用at设置定时任务,它首先检查/etc/at.allow,如果文件存在,则只有文件中列出的用户(每行一个用户名),才能使用at;如果不存在,则检查文件/etc/at.deny,不在此文件中的所有用户都可以使用at。如果/etc/at.deny是空文件,则表示系统中所有用户都可以使用at;如果/etc/at.deny文件也不存在,则只有超级用户(root)才能使用at。



https://blog.sciencenet.cn/blog-3222255-1156165.html

上一篇:Win 10 下 Universal-USB-Installer-1.9.6.7 安装ubuntu16.04
下一篇:Ubuntu 更改默认终端设置为terminator
收藏 IP: 211.68.2.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-24 11:57

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部