在計(jì)算機(jī)的使用過(guò)程中,經(jīng)常會(huì)有一些計(jì)劃中的任務(wù)需要在將來(lái)的某個(gè)時(shí)間執(zhí)行,linux中提供了一些方法來(lái)設(shè)定定時(shí)任務(wù)。
1、at
命令at從文件或標(biāo)準(zhǔn)輸入中讀取命令并在將來(lái)的一個(gè)時(shí)間執(zhí)行,只執(zhí)行一次。at的正常執(zhí)行需要有守護(hù)進(jìn)程atd:
#安裝atyum install -y at 或 apt-get install at -y#啟動(dòng)守護(hù)進(jìn)程service atd start 或 systemctl start atd#查看是否開(kāi)機(jī)啟動(dòng)(關(guān)于systemctl請(qǐng)看這一篇)chkconfig --list|grep atd 或 systemctl list-unit-files|grep atd#設(shè)置開(kāi)機(jī)啟動(dòng)chkconfig --level 235 atd on 或 systemctl enable atd
如果不使用管道|或指定選項(xiàng)-f的話,at的執(zhí)行將會(huì)是交互式的,需要在at的提示符下輸入命令:
[root@centos7 temp]# at now +2 minutes #執(zhí)行at并指定執(zhí)行時(shí)刻為現(xiàn)在時(shí)間的后兩分鐘at> echo hello world > /root/temp/file #手動(dòng)輸入命令并回車at> <EOT> #ctrl+d 結(jié)束輸入job 9 at Thu Dec 22 14:05:00 2016 #顯示任務(wù)號(hào)及執(zhí)行時(shí)間[root@centos7 temp]#
選項(xiàng)-l或命令atq查詢?nèi)蝿?wù)
[root@centos7 temp]# atq9 Thu Dec 22 14:05:00 2016 a root
到達(dá)時(shí)間后任務(wù)被執(zhí)行,生成一個(gè)新文件file并保存echo的輸出內(nèi)容
[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指定時(shí)間的方法很豐富,可以是
1)hh:mm小時(shí):分鐘(當(dāng)天,如果時(shí)間已過(guò),則在第二天執(zhí)行)
2)midnight(深夜),noon(中午),teatime(下午茶時(shí)間,下午4點(diǎn)),today,tomorrow等
3)12小時(shí)計(jì)時(shí)制,時(shí)間后加am(上午)或pm(下午)
4)指定具體執(zhí)行日期mm/dd/yy(月/日/年)或dd.mm.yy(日.月.年)
5)相對(duì)計(jì)時(shí)法now + n units,now是現(xiàn)在時(shí)刻,n為數(shù)字,units是單位(minutes、hours、days、weeks)
如明天下午2點(diǎn)20分執(zhí)行創(chuàng)建一個(gè)目錄
[root@centos7 temp]# at 02:20pm tomorrowat> mkdir /root/temp/Xat> <EOT>job 11 at Fri Dec 23 14:20:00 2016
選項(xiàng)-d或命令atrm表示刪除任務(wù)
[root@centos7 temp]# at -d 11 #刪除11號(hào)任務(wù)(上例)[root@centos7 temp]# atq[root@centos7 temp]#
可以使用管道|或選項(xiàng)-f讓at從標(biāo)準(zhǔn)輸入或文件中獲得任務(wù)
[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通過(guò)兩個(gè)文件/etc/at.allow和/etc/at.deny來(lái)決定系統(tǒng)中哪些用戶可以使用at設(shè)置定時(shí)任務(wù),它首先檢查/etc/at.allow,如果文件存在,則只有文件中列出的用戶(每行一個(gè)用戶名),才能使用at;如果不存在,則檢查文件/etc/at.deny,不在此文件中的所有用戶都可以使用at。如果/etc/at.deny是空文件,則表示系統(tǒng)中所有用戶都可以使用at;如果/etc/at.deny文件也不存在,則只有超級(jí)用戶(root)才能使用at。
新聞熱點(diǎn)
疑難解答
網(wǎng)友關(guān)注