linux 定時任務at與crontab
1、at
一次性任務
1.1 命令at
安裝
從文件或標準輸入中讀取命令並在將來的一個時間執行,只執行一次。at
的正常執行需要有守護進程atd.
#安裝at yum install -y at #啟動守護進程 service atd start #查看是否開機啟動 chkconfig --list|grep atd #設置開機啟動 chkconfig --level 235 atd on
1.2 使用
如果不使用管道|
或指定選項-f
的話,at
的執行將會是交互式的,需要在at的提示符下輸入命令:
[[email protected] ~]# at now +2 minutes #執行at並指定執行時刻為現在時間的後兩分鐘 at> echo hello world > /root/a.txt #手動輸入命令並回車 at> <EOT> #ctrl+d 結束輸入 job 2 at 2017-07-24 16:08 #顯示任務號及執行時間
選項-l
或命令atq
查詢任務
[[email protected] ~]# atq 2 2017-07-24 16:21 a root [[email protected] ~]# at -l 2 2017-07-24 16:21 a root
到達時間後任務被執行,生成一個新文件file並保存echo的輸出內容
[[email protected] ~]# cat a.txt hello world
at
指定時間的方法:
1)hh:mm
小時:分鐘(當天,如果時間已過,則在第二天執行)
2)midnight
(深夜),noon
(中午),teatime
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分執行創建一個目錄
[[email protected]]# at 02:20pm tomorrow at> mkdir /root/temp/x at> <EOT> job 11 at Fri Dec 23 14:20:00 2016
選項-d
atrm
表示刪除任務[[email protected]]# at -d 11 #刪除11號任務(上例) [[email protected]]# atq [[email protected]]#
可以使用管道|
或選項-f
讓at
從標準輸入或文件中獲得任務
[[email protected]~]# cat test.txt echo hello world > /root/temp/file [[email protected]~] at -f test.txt 5pm +2 days job 12 at Sat Dec 24 17:00:00 2016 [[email protected]~]# cat test.txt|at 16:20 12/23/16 job 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。
2、crontab
系統中每個用戶都可以擁有自己的cron table
,同atd
類似,crond
也有兩個文件/etc/cron.allow
和/etc/cron.deny
用來限制用戶使用cron,規則也和atd
的兩個文件相同。
/etc/cron.deny 表示不能使用crontab 命令的用戶
/etc/cron.allow 表示能使用crontab的用戶。
如果兩個文件同時存在,那麽/etc/cron.allow 優先。
如果兩個文件都不存在,那麽只有超級用戶可以安排作業。
每個用戶都會生成一個自己的crontab 文件。這些文件在/var/spool/cron目錄下
crontab -u 指定一個用戶
crontab -l 列出某個用戶的任務計劃
crontab -r 刪除某個用戶的任務
crontab -e 編輯某個用戶的任務
對於系統級別的定時任務,這些任務更加重要,大部分linux系統在/etc中包含了一系列與 cron有關的子目錄:/etc/cron.{hourly,daily,weekly,monthly}
,目錄中的文件定義了每小時、每天、每周、每月需要運行的腳本,運行這些任務的精確時間在文件/etc/crontab
中指定。如:
[[email protected] spool]# cat /etc/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 # run-parts 01 * * * * root run-parts /etc/cron.hourly 02 4 * * * root run-parts /etc/cron.daily 22 4 * * 0 root run-parts /etc/cron.weekly 42 4 1 * * root run-parts /etc/cron.monthly 第一部分表示分鐘(0-59),* 表示每分鐘 第二部分表示小時(0-23),* 表示每小時 第三部分表示日(1-31), * 表示每天 第四部分表示月(1-12), * 表示每月 第五部分表示周幾(0-6,0表示周日),* 表示一周中每天 第六部分表示要執行的任務
anacron
的目的並不是完全替代cron
,是作為cron
的一個補充。anacron
的任務定義在文件/etc/anacrontab
中:
[[email protected] spool]# cat /etc/anacrontab # /etc/anacrontab: configuration file for anacron # See anacron(8) and anacrontab(5) for details. SHELL=/bin/sh PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root # the maximal random delay added to the base delay of the jobs RANDOM_DELAY=45 # the jobs will be started during the following hours only START_HOURS_RANGE=3-22 #period in days delay in minutes job-identifier command 1 5 cron.daily nice run-parts /etc/cron.daily 7 25 cron.weekly nice run-parts /etc/cron.weekly @monthly 45 cron.monthly nice run-parts /etc/cron.monthly
本文出自 “獨上高樓,望盡天涯路” 博客,請務必保留此出處http://nxyboy.blog.51cto.com/10511646/1950598
linux 定時任務at與crontab