欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

linux使用crontab實(shí)現(xiàn)PHP執(zhí)行計(jì)劃定時(shí)任務(wù)

 更新時(shí)間:2014年05月10日 17:00:37   作者:  
前幾天寫過一篇文章,利用單純的php實(shí)現(xiàn)定時(shí)執(zhí)行任務(wù),但是效率不佳,對(duì)于linux來說用crontab實(shí)現(xiàn)更加合理

首先說說cron,它是一個(gè)linux下的定時(shí)執(zhí)行工具。根用戶以外的用戶可以使用 crontab 工具來配置 cron 任務(wù)。所有用戶定義的 crontab 都被保存在/var/spool/cron 目錄中,并使用創(chuàng)建它們的用戶身份來執(zhí)行。要以某用戶身份創(chuàng)建一個(gè) crontab 項(xiàng)目,登錄為該用戶,然后鍵入 crontab -e 命令來編輯該用戶的 crontab。該文件使用的格式和 /etc/crontab 相同。當(dāng)對(duì) crontab 所做的改變被保存后,該 crontab 文件就會(huì)根據(jù)該用戶名被保存,并寫入文件 /var/spool/cron/username 中。cron 守護(hù)進(jìn)程每分鐘都檢查 /etc/crontab 文件、etc/cron.d/ 目錄、以及 /var/spool/cron 目錄中的改變。如果發(fā)現(xiàn)了改變,它們就會(huì)被載入內(nèi)存。這樣,當(dāng)某個(gè) crontab 文件改變后就不必重新啟動(dòng)守護(hù)進(jìn)程了。

安裝crontab:

yum install crontabs

說明:
/sbin/service crond start //啟動(dòng)服務(wù)
/sbin/service crond stop //關(guān)閉服務(wù)
/sbin/service crond restart //重啟服務(wù)
/sbin/service crond reload //重新載入配置

查看crontab服務(wù)狀態(tài):service crond status

手動(dòng)啟動(dòng)crontab服務(wù):service crond start

查看crontab服務(wù)是否已設(shè)置為開機(jī)啟動(dòng),執(zhí)行命令:ntsysv

加入開機(jī)自動(dòng)啟動(dòng):
chkconfig –level 35 crond on

crontab命令:

功能說明:設(shè)置計(jì)時(shí)器。

語  法:crontab [-u <用戶名稱>][配置文件] 或 crontab [-u <用戶名稱>][-elr]

補(bǔ)充說明:cron是一個(gè)常駐服務(wù),它提供計(jì)時(shí)器的功能,讓用戶在特定的時(shí)間得以執(zhí)行預(yù)設(shè)的指令或程序。只要用戶會(huì)編輯計(jì)時(shí)器的配置文件,就可以使 用計(jì)時(shí)器的功能。其配置文件格式如下:
Minute Hour Day Month DayOFWeek Command

參  數(shù):
-e  編輯該用戶的計(jì)時(shí)器設(shè)置。
-l  列出該用戶的計(jì)時(shí)器設(shè)置。
-r  刪除該用戶的計(jì)時(shí)器設(shè)置。
-u<用戶名稱>  指定要設(shè)定計(jì)時(shí)器的用戶名稱。

crontab 格式:

基本格式 :

分鐘   小時(shí)   日   月   星期   命令

*        *      *    *     *       *

第1列表示分鐘1~59 每分鐘用*或者 */1表示
第2列表示小時(shí)1~23(0表示0點(diǎn))
第3列表示日期1~31
第4列 表示月份1~12
第5列標(biāo)識(shí)號(hào)星期0~6(0表示星期天)
第6列要運(yùn)行的命令

記住幾個(gè)特殊符號(hào)的含義:
“*”代表取值范圍內(nèi)的數(shù)字,
“/”代表”每”,
“-”代表從某個(gè)數(shù)字到某個(gè)數(shù)字,
“,”分開幾個(gè)離散的數(shù)字

# Use the hash sign to prefix a comment
# +—————- minute (0 – 59)
# | +————- hour (0 – 23)
# | | +———- day of month (1 – 31)
# | | | +——- month (1 – 12)
# | | | | +—- day of week (0 – 7) (Sunday=0 or 7)
# | | | | |
# * * * * * command to be executed

crontab幾個(gè)例子如下:

(1)第一個(gè)例子。

30 21 * * * /etc/init.d/nginx restart
每晚的21:30重啟 nginx。

(2)第二個(gè)例子,也就是本教程測(cè)試的例子

* * * * * /usr/bin/php -f /root/test.php >> test.log

每一分鐘執(zhí)行/root/test.php文件,將結(jié)果輸出到test.log中。

完成了上面基礎(chǔ)工作后,就來看看怎么使用crontab定時(shí)執(zhí)行PHP腳本:

(1)我在/root下新建test.php文件,內(nèi)容如下:

復(fù)制代碼 代碼如下:

<?php
        #!/usr/bin/php -q
        echo  date('Y-m-d H:i:s')."from http://www.phpddt.com \n";
?>

說明:你可以用whereis php查找php執(zhí)行文件位置。


(2)然后crontab -e編寫如下shell:

復(fù)制代碼 代碼如下:

* * * * * /usr/bin/php -f /root/test.php >> test.log

說明:test.php必須為可執(zhí)行文件:chmod +x test.php

測(cè)試結(jié)果很正常,截圖如下:

當(dāng)然你可以用使用crontab -e繼續(xù)添加任務(wù),在/var/spool/cron下你可以看到一個(gè)root文件。
windows下直接用windows計(jì)劃任務(wù),通過bat打開網(wǎng)頁就可以了。不像linux這么復(fù)制。

相關(guān)文章

最新評(píng)論