日期:2014-05-16  浏览次数:21035 次

Linux 涓?crontab 璇﹁В鍙婄ず渚?杞?

cron鏄竴涓猯inux涓嬬殑瀹氭椂鎵ц宸ュ叿锛屽彲浠ュ湪鏃犻渶浜哄伐骞查鐨勬儏鍐典笅杩愯浣滀笟銆傜敱浜嶤ron 鏄疞inux鐨勫唴缃湇鍔★紝浣嗗畠涓嶈嚜鍔ㄨ捣鏉ワ紝鍙互鐢ㄤ互涓嬬殑鏂规硶鍚姩銆佸叧闂繖涓湇鍔★細

聽/sbin/service crond start //鍚姩鏈嶅姟
/sbin/service crond stop //鍏抽棴鏈嶅姟
/sbin/service crond restart //閲嶅惎鏈嶅姟
/sbin/service crond reload //閲嶆柊杞藉叆閰嶇疆

浣犱篃鍙互灏嗚繖涓湇鍔″湪绯荤粺鍚姩鐨勬椂鍊欒嚜鍔ㄥ惎鍔細
鍦?etc/rc.d/rc.local杩欎釜鑴氭湰鐨勬湯灏惧姞涓婏細
/sbin/service crond start

鐜板湪Cron杩欎釜鏈嶅姟宸茬粡鍦ㄨ繘绋嬮噷闈簡锛屾垜浠氨鍙互鐢ㄨ繖涓湇鍔′簡锛孋ron鏈嶅姟鎻愪緵浠ヤ笅鍑犵鎺ュ彛渚涘ぇ瀹朵娇鐢細

聽1銆佺洿鎺ョ敤crontab鍛戒护缂栬緫

聽cron鏈嶅姟鎻愪緵crontab鍛戒护鏉ヨ瀹歝ron鏈嶅姟鐨勶紝浠ヤ笅鏄繖涓懡浠ょ殑涓€浜涘弬鏁颁笌璇存槑锛?

crontab -u //璁惧畾鏌愪釜鐢ㄦ埛鐨刢ron鏈嶅姟锛屼竴鑸瑀oot鐢ㄦ埛鍦ㄦ墽琛岃繖涓懡浠ょ殑鏃跺€欓渶瑕佹鍙傛暟
crontab -l //鍒楀嚭鏌愪釜鐢ㄦ埛cron鏈嶅姟鐨勮缁嗗唴瀹?
crontab -r //鍒犻櫎鏌愪釜鐢ㄦ埛鐨刢ron鏈嶅姟
crontab -e //缂栬緫鏌愪釜鐢ㄦ埛鐨刢ron鏈嶅姟


姣斿璇磖oot鏌ョ湅鑷繁鐨刢ron璁剧疆锛歝rontab -u root -l
鍐嶄緥濡傦紝root鎯冲垹闄red鐨刢ron璁剧疆锛歝rontab -u fred -r
鍦ㄧ紪杈慶ron鏈嶅姟鏃讹紝缂栬緫鐨勫唴瀹规湁涓€浜涙牸寮忓拰绾﹀畾锛岃緭鍏ワ細crontab -u root -e
杩涘叆vi缂栬緫妯″紡锛岀紪杈戠殑鍐呭涓€瀹氳绗﹀悎涓嬮潰鐨勬牸寮忥細*/1 * * * * ls >> /tmp/ls.txt

杩欎釜鏍煎紡鐨勫墠涓€閮ㄥ垎鏄鏃堕棿鐨勮瀹氾紝鍚庨潰涓€閮ㄥ垎鏄鎵ц鐨勫懡浠わ紝濡傛灉瑕佹墽琛岀殑鍛戒护澶锛屽彲浠ユ妸杩欎簺鍛戒护鍐欏埌涓€涓剼鏈噷闈紝鐒跺悗鍦ㄨ繖閲岀洿鎺ヨ皟鐢ㄨ繖涓剼鏈氨鍙互浜嗭紝璋冪敤鐨勬椂鍊欒寰楀啓鍑哄懡浠ょ殑瀹屾暣璺緞銆傛椂闂寸殑璁惧畾鎴戜滑鏈変竴瀹氱殑绾﹀畾锛屽墠闈簲涓?鍙蜂唬琛ㄤ簲涓暟瀛楋紝鏁板瓧鐨勫彇鍊艰寖鍥村拰鍚箟濡備笅锛?

聽鍒嗛挓銆€锛?-59锛?
灏忔檪銆€锛?-23锛?
鏃ユ湡銆€锛?-31锛?
鏈堜唤銆€锛?-12锛?
鏄熸湡銆€锛?-6锛?/0浠h〃鏄熸湡澶?

聽闄や簡鏁板瓧杩樻湁鍑犱釜涓壒娈婄殑绗﹀彿灏辨槸"*"銆?/"鍜?-"銆?,"锛?浠h〃鎵€鏈夌殑鍙栧€艰寖鍥村唴鐨勬暟瀛楋紝"/"浠h〃姣忕殑鎰忔€?"*/5"琛ㄧず姣?涓崟浣嶏紝"-"浠h〃浠庢煇涓暟瀛楀埌鏌愪釜鏁板瓧,","鍒嗗紑鍑犱釜绂绘暎鐨勬暟瀛椼€備互涓嬩妇鍑犱釜渚嬪瓙璇存槑闂锛?

姣忓ぉ鏃╀笂6鐐?

0 6 * * * echo "Good morning." >> /tmp/test.txt //娉ㄦ剰鍗曠函echo锛屼粠灞忓箷涓婄湅涓嶅埌浠讳綍杈撳嚭锛屽洜涓篶ron鎶婁换浣曡緭鍑洪兘email鍒皉oot鐨勪俊绠变簡銆?

姣忎袱涓皬鏃?

0 */2 * * * echo "Have a break now." >> /tmp/test.txt

鏅氫笂11鐐瑰埌鏃╀笂8鐐逛箣闂存瘡涓や釜灏忔椂锛屾棭涓婂叓鐐?

0 23-7/2锛? * * * echo "Have a good dream锛氾級" >> /tmp/test.txt

姣忎釜鏈堢殑4鍙峰拰姣忎釜绀兼嫓鐨勭ぜ鎷滀竴鍒扮ぜ鎷滀笁鐨勬棭涓?1鐐?

0 11 4 * 1-3 command line

1鏈?鏃ユ棭涓?鐐?

0 4 1 1 * command line

姣忔缂栬緫瀹屾煇涓敤鎴风殑cron璁剧疆鍚庯紝cron鑷姩鍦?var/spool/cron涓嬬敓鎴愪竴涓笌姝ょ敤鎴峰悓鍚嶇殑鏂囦欢锛屾鐢ㄦ埛鐨刢ron淇℃伅閮借褰曞湪杩欎釜鏂囦欢涓紝杩欎釜鏂囦欢鏄笉鍙互鐩存帴缂栬緫鐨勶紝鍙彲浠ョ敤crontab -e 鏉ョ紪杈戙€俢ron鍚姩鍚庢瘡杩囦竴浠介挓璇讳竴娆¤繖涓枃浠讹紝妫€鏌ユ槸鍚﹁鎵ц閲岄潰鐨勫懡浠ゃ€傚洜姝ゆ鏂囦欢淇敼鍚庝笉闇€瑕侀噸鏂板惎鍔╟ron鏈嶅姟銆?

2銆佺紪杈?etc/crontab 鏂囦欢閰嶇疆cron

cron鏈嶅姟姣忓垎閽熶笉浠呰璇讳竴娆?var/spool/cron鍐呯殑鎵€鏈夋枃浠讹紝杩橀渶瑕佽涓€娆?etc/crontab,鍥犳鎴戜滑閰嶇疆杩欎釜鏂囦欢涔熻兘杩愮敤 cron鏈嶅姟鍋氫竴浜涗簨鎯呫€傜敤crontab閰嶇疆鏄拡瀵规煇涓敤鎴风殑锛岃€岀紪杈?etc/crontab鏄拡瀵圭郴缁熺殑浠诲姟銆傛鏂囦欢鐨勬枃浠舵牸寮忔槸锛?

SHELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=root聽聽聽聽聽 //濡傛灉鍑虹幇閿欒锛屾垨鑰呮湁鏁版嵁杈撳嚭锛屾暟鎹綔涓洪偖浠跺彂缁欒繖涓笎鍙?

HOME=/聽聽聽 //浣跨敤鑰呰繍琛岀殑璺緞,杩欓噷鏄牴鐩綍

# run-parts

01 * * * * root run-parts /etc/cron.hourly //姣忓皬鏃舵墽琛?etc/cron.hourly鍐呯殑鑴氭湰
02 4 * * * root run-parts /etc/cron.daily //姣忓ぉ鎵ц/etc/cron.daily鍐呯殑鑴氭湰
22 4 * * 0 root run-parts /etc/cron.weekly //姣忔槦鏈熸墽琛?etc/cron.weekly鍐呯殑鑴氭湰
42 4 1 * * root run-parts /etc/cron.monthly //姣忔湀鍘绘墽琛?etc/cron.monthly鍐呯殑鑴氭湰

澶у娉ㄦ剰"run-parts"杩欎釜鍙傛暟浜嗭紝濡傛灉鍘绘帀杩欎釜鍙傛暟鐨勮瘽锛屽悗闈㈠氨鍙互鍐欒杩愯鐨勬煇涓剼鏈悕锛岃€屼笉鏄枃浠跺す鍚嶄簡銆?/p>

--------------------------------------
鍩烘湰鏍煎紡 :
*銆€銆€*銆€銆€*銆€銆€*銆€銆€*銆€銆€command
鍒嗐€€鏃躲€€鏃ャ€€鏈堛€€鍛ㄣ€€鍛戒护

绗?鍒楄〃绀哄垎閽?锝?9 姣忓垎閽熺敤*鎴栬€?*/1琛ㄧず
绗?鍒楄〃绀哄皬鏃?锝?3锛?琛ㄧず0鐐癸級
绗?鍒楄〃绀烘棩鏈?锝?1
绗?鍒楄〃绀烘湀浠?锝?2
绗?鍒楁爣璇嗗彿鏄熸湡0锝?锛?琛ㄧず鏄熸湡澶╋級
绗?鍒楄杩愯鐨勫懡浠?/p>

聽crontab鏂囦欢鐨勪竴浜涗緥瀛愶細

聽30 21 * * * /usr/local/etc/rc.d/lighttpd restart
涓婇潰鐨勪緥瀛愯〃绀烘瘡鏅氱殑21:30閲嶅惎lighttpd 銆?/p>

聽45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart
涓婇潰鐨勪緥瀛愯〃绀烘瘡鏈?銆?0銆?2鏃ョ殑4 : 45閲嶅惎lighttpd 銆?/p>

聽10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart
涓婇潰鐨勪緥瀛愯〃绀烘瘡鍛ㄥ叚銆佸懆鏃ョ殑1 : 10閲嶅惎lighttpd 銆?/p>

聽0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart
涓婇潰鐨勪緥瀛愯〃绀哄湪姣忓ぉ18 : 00鑷?3 : 00涔嬮棿姣忛殧30鍒嗛挓閲嶅惎lighttpd 銆?/p>

聽0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart
涓婇潰鐨勪緥瀛愯〃绀烘瘡鏄熸湡鍏殑11 : 00 pm閲嶅惎lighttpd 銆?/p>

* */1 * * * /usr/local/etc/rc.d/lighttpd restart
姣忎竴灏忔椂閲嶅惎lighttpd

聽* 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart
鏅氫笂11鐐瑰埌鏃╀笂7鐐