crond定时任务详细分析
一、定時任務crond的介紹 ?
crond是linux系統(tǒng)中用來定期執(zhí)行命令或指定程序任務的一種服務或軟件。一般情況下,我們安裝文成系統(tǒng)之后,默認變回啟動crond任務調(diào)度服務,crond服務會定期(默認每分鐘檢查一次)檢查系統(tǒng)中是否有要執(zhí)行的任務工作。如果有,變會根據(jù)預先設定的定時任務自動執(zhí)行該定時任務,就如同生活中的鬧鐘一樣。
[root@iZ94lm56da9Z?~]#?chkconfig?--list|grep?crond????<--查詢是否開啟 crond??????????????0:off????1:off????2:on????3:on????4:on????5:on????6:off [root@iZ94lm56da9Z?~]#?crontab?-l????<--查詢crond的配置 no?crontab?for?root因為crond是每分鐘級別,如果crond服務搞不定了,一般工作中寫腳本守護程序進程:
[root@iZ94lm56da9Z?~]#?cat?corn? while?true doecho?"wangde?.....Rui"sleep?1 done #內(nèi)容意思為每秒echo出"wangde?.....Rui"。補充兩個概念:
????程序文件:程序代碼組成,但是沒有在計算機內(nèi)執(zhí)行。當前沒有執(zhí)行。
????守護程序或者守護進程:進程就是在計算機中正在執(zhí)行的程序;守護進程就是一直運行的程序。
可以看出定時任務是一個守護進程。
[root@iZ94lm56da9Z?~]#?ps?-ef?|grep?crond root???????923?????1??0?Aug25??????????00:00:00?crond root?????27961?27915??0?15:29?pts/0????00:00:00?grep?crondcrond和ssh一樣是一直在后臺運行的守護進程:
[root@iZ94lm56da9Z?~]#?/etc/init.d/sshd?restart Stopping?sshd:?????????????????????????????????????????????[??OK??] Starting?sshd:?????????????????????????????????????????????[??OK??] [root@iZ94lm56da9Z?~]#?/etc/init.d/crond?restart Stopping?crond:????????????????????????????????????????????[??OK??] Starting?crond:????????????????????????????????????????????[??OK??]二、不同系統(tǒng)的定時任務
windows的定時任務存放位置:所有程序->附件->系統(tǒng)工具->任務計劃程序
Linux系統(tǒng)的定時任務調(diào)度的工作可以分為以下兩種情況:
????1.linux系統(tǒng)自身定期執(zhí)行的任務工作:系統(tǒng)周期性的自行執(zhí)行的任務工作,例如輪循備份系統(tǒng)日志、備份系統(tǒng)數(shù)據(jù)、清理系統(tǒng)緩存等,這些任務無需我們?nèi)藶楦深A。如下:
[root@alone?~]#?ll?/var/log/messages* -rw-------??1?root?root?113765?8月??23?23:49?/var/log/messages -rw-------.?1?root?root?226986?8月???7?02:58?/var/log/messages-20160807 -rw-------??1?root?root??25713?8月??14?02:58?/var/log/messages-20160814 -rw-------??1?root?root??25649?8月??21?02:58?/var/log/messages-20160821 [root@alone?~]#?ll?/var/log/secure* -rw-------??1?root?root??43166?8月??23?23:44?/var/log/secure -rw-------.?1?root?root??91198?8月???7?03:16?/var/log/secure-20160807 -rw-------??1?root?root?133631?8月??14?03:46?/var/log/secure-20160814 -rw-------??1?root?root?134102?8月??21?03:30?/var/log/secure-20160821 #每周一份日志,系統(tǒng)自身執(zhí)行的定時任務系統(tǒng)默認執(zhí)行的日志腳本存放位置:
[root@alone?~]#?cd?/etc/logrotate.d/ [root@alone?logrotate.d]#?pwd /etc/logrotate.d [root@alone?logrotate.d]#?ls?-lrt 總用量?32 -rw-r--r--.?1?root?root?136?8月??23?2010?ppp -rw-r--r--.?1?root?root?329?7月??17?2012?psacct -rw-r--r--.?1?root?root?100?2月???4?2013?wpa_supplicant -rw-r--r--.?1?root?root?100?2月??22?2013?yum -rw-r--r--.?1?root?root?210?8月??15?2013?syslog -rw-r--r--.?1?root?root??71?8月??17?2013?cups -rw-r--r--.?1?root?root?219?11月?23?2013?sssd -rw-r--r--.?1?root?root?103?11月?26?2013?dracut [root@alone?logrotate.d]#? [root@alone?logrotate.d]#?less?syslog? [root@alone?logrotate.d]#?cat?syslog?|grep?-v?'^$' /var/log/cron /var/log/maillog /var/log/messages /var/log/secure /var/log/spooler {sharedscriptspostrotate/bin/kill?-HUP?`cat?/var/run/syslogd.pid?2>?/dev/null`?2>?/dev/null?||?trueendscript }系統(tǒng)自動輪循任務的設置配置路徑:
[root@alone?logrotate.d]#?ll?/etc/?|grep?cron -rw-------.??1?root?root????541?11月?23?2013?anacrontab drwxr-xr-x.??2?root?root???4096?7月??27?13:14?cron.d drwxr-xr-x.??2?root?root???4096?7月??27?13:17?cron.daily -rw-------.??1?root?root??????0?11月?23?2013?cron.deny drwxr-xr-x.??2?root?root???4096?7月??27?13:13?cron.hourly drwxr-xr-x.??2?root?root???4096?7月??27?13:15?cron.monthly -rw-r--r--.??1?root?root????457?9月??27?2011?crontab drwxr-xr-x.??2?root?root???4096?9月??27?2011?cron.weekly [root@alone?logrotate.d]#?pwd /etc/logrotate.d以下是系統(tǒng)及的定時任務,一般用戶自定義的定時任務不會放在這里:
????2.用戶執(zhí)行的定時任務:某個用戶或者系統(tǒng)管理員定期要做的任務工作,例如每個5分鐘和互聯(lián)網(wǎng)時間服務器同步(當然這個是最基礎的系統(tǒng)優(yōu)化),每天晚上0點備份站點數(shù)據(jù)及數(shù)據(jù)庫數(shù)據(jù),這些工作一般都是用戶自己設定定時任務才行,
例如:服務器時間同步:
[root@iZ94lm56da9Z?~]#?crontab?-l */5?*?*?*?*?/usr/sbin/ntpdate?time.nist.gov?>/dev/null?2>&1三、linux系統(tǒng)下定時任務軟件的種類
linux系統(tǒng)下基本有以下幾種:at(適合只執(zhí)行一次)、crontab(最常用)、anacron。
????at:適合執(zhí)行一次就借宿的調(diào)度任務命令。例如:在今晚需要處理一個任務,僅僅是今天晚上,屬于突發(fā)性的工作任務。要執(zhí)行at命令,還需要啟動一個名為atd的服務才行。(在工作中不常用,所以默認為關閉狀態(tài))
[root@iZ94lm56da9Z?~]#?chkconfig?--list?|grep?atd atd????????????????0:off????1:off????2:off????3:off????4:off????5:off????6:off????anacron:這個命令主要用于非7*24小時開機的服務器準備的,anacron并不能指定具體時間執(zhí)行任務,而是以天為周期或者在系統(tǒng)每次開機后執(zhí)行的任務工作。他會檢測服務器停機期間應該執(zhí)行但沒有進行的任務工作,并將該任務執(zhí)行一遍。(工作中一般服務器是持續(xù)開機,所以一般不怎么用)
????備注:
1.這里的crond服務是運行的程序,而crontab命令用戶用來設置定時規(guī)則的命令
2.crond服務是工作沖常用的重要服務,at和anacron很少使用,可以忽略,
3.工作中幾乎每個服務器都會用到crond服務。
四、crontab命令的格式 (下面進入主題)
crond????守護進程,一直運行著的
crontab????設置命令,-e 編輯;-l 列表。
[root@alone?logrotate.d]#?crontab?-h crontab:無效選項?--?h crontab:?usage?error:?unrecognized?option usage:????crontab?[-u?user]?filecrontab?[-u?user]?[?-e?|?-l?|?-r?](default?operation?is?replace,?per?1003.2)-e????(edit?user's?crontab)-l????(list?user's?crontab)-r????(delete?user's?crontab)-i????(prompt?before?deleting?user's?crontab)-s????(selinux?context)crontab -e? 相當于 vi /var/spool/cron/root
crontab -l?? 相當于 cat /var/spool/cron/root
[root@iZ94lm56da9Z?~]#?crontab?-l */5?*?*?*?*?/usr/sbin/ntpdate?time.nist.gov?>/dev/null?2>&1 [root@iZ94lm56da9Z?~]#?cat?/var/spool/cron/root? */5?*?*?*?*?/usr/sbin/ntpdate?time.nist.gov?>/dev/null?2>&1讓普通用戶執(zhí)行crontab -u richy -e?? 或者 切換到richy用戶下 再使用crontab
crontab 定時任務命令字段解釋
*?????????? *??? ? ?? *??? ? ?? *?? ? ? ? ? *
0-59?? 0-23?? 1-31?? 1-12???? 0-6
*? 每?? * * * * * /bin/sh/scripts/richy.sh? 每分鐘執(zhí)行richy.sh
-? 范圍??? 00 17-19 * * * /bin/sh/scripts/richy.sh? 每天17點,18點,19點整執(zhí)行richy.sh
, 分隔?? 30 17,18,19 * * * /bin/sh/scripts/richy.sh? 每天17:30,18:30,19:30執(zhí)行richy.sh
/n? 每單位時間?? */10 * * * * /bin/sh/scripts/richy.sh? 每10分鐘執(zhí)行richy.sh
30 3-5 17-19 * * /bin/sh/scripts/richy.sh? 每月的17,18,19號的3:30,4:30,5:30執(zhí)行命令richy.sh
五、crontab編輯定時任務依賴的服務
[root@iZ94lm56da9Z?~]#?chkconfig?--list?|grep?crond crond??????????????0:off????1:off????2:on????3:on????4:on????5:on????6:off [root@iZ94lm56da9Z?~]#?ps?-ef?|grep?crond?|grep?-v?grep root?????27988?????1??0?15:30??????????00:00:00?crond六、crondtab的總結
定時任務要加注釋;
結尾不要有>/dev/null 2>&1;
如果存在目錄,目錄必須要存在,因為crontab不會創(chuàng)建新目錄;
定時任務中的路徑一定要是絕對路徑;
crond服務必須開啟并運行;
.
.
.
.
轉載于:https://blog.51cto.com/richylu/1843315
總結
以上是生活随笔為你收集整理的crond定时任务详细分析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: kernel shell bash简介
- 下一篇: Ansible 一步一步从入门到精通(一