1. 程式人生 > >Linux定時任務Crontab執行PHP指令碼

Linux定時任務Crontab執行PHP指令碼

http://blog.chinaunix.net/uid-7552018-id-182133.html crontab執行php指令碼 http://www.jb51.net/article/29136.htm   複製程式碼
[[email protected] ~]# crontab -u root -l
no crontab for root
[[email protected] ~]# crontab -u root -e
no crontab for root - using an empty one
crontab: installing new crontab
[
[email protected]
~]# crontab -u root -l */1 * * * * ls >> /tmp/ls.txt [[email protected] ~]#
複製程式碼

 */1 * * * * /usr/bin/php /var/www/html/testcrontab.php

複製程式碼
<?php
$myfile = fopen("/var/www/html/newfile.txt", "a+");//這裡寫相對路徑不行
//or die("Unable to open file!!");
$txt = "Bill Gates\n";
fwrite($myfile, $txt); $txt = "Steve Jobs\n"; fwrite($myfile, $txt); fclose($myfile); ?>
複製程式碼

 

[[email protected] ~]# /usr/bin/php /var/www/html/xiaochu/test/testGame.php
PHP Warning: require_once(../Amfphp/app/util/Mpdo.class.php): failed to open stream: No such file or directory in /var/www/html/xiaochu/test/testGame.php on line 2
PHP Fatal error: require_once(): Failed opening required '../Amfphp/app/util/Mpdo.class.php' (include_path='.:/usr/share/pear:/usr/share/php') in /var/www/html/xiaochu/test/testGame.php on line 2
[

[email protected] ~]# crontab -l
*/1 * * * * /usr/bin/php /var/www/html/xiaochu/test/testGame.php
You have new mail in /var/spool/mail/root
[[email protected] ~]#

/usr/bin/php 這樣執行的話好像不認相對路徑,只能通過URL方式定時執行了

複製程式碼
[[email protected] ~]# /etc/init.d/mysqld start
Another MySQL daemon already running with the same unix socket.
正在啟動 mysqld:                                          [失敗]
[[email protected] ~]# /etc/init.d/mysqld start
正在啟動 mysqld:                                          [確定]
[[email protected] ~]# 
複製程式碼

刪除/var/lib/mysql/mysql.sock 檔案就行了

************************************************************************************ cron是一個linux下的定時執行工具,可以在無需人工干預的情況下執行作業。由於Cron 是Linux的內建服務,但它不自動起來,可以用以下的方法啟動、關閉這個服務:   /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這個服務已經在程序裡面了,我們就可以用這個服務了,Cron服務提供以下幾種介面供大家使用:   1.直接用crontab命令編輯   cron服務提供crontab命令來設定cron服務的,以下是這個命令的一些引數與說明:   crontab -u //設定某個使用者的cron服務,一般root使用者在執行這個命令的時候需要此引數 crontab -l //列出某個使用者cron服務的詳細內容 crontab -r //刪除沒個使用者的cron服務 crontab -e //編輯某個使用者的cron服務   比如說root檢視自己的cron設定:crontab -u root -l 再例如,root想刪除fred的cron設定:crontab -u fred -r 在編輯cron服務時,編輯的內容有一些格式和約定,輸入:crontab -u root -e   進入vi編輯模式,編輯的內容一定要符合下面的格式:*/1 * * * * ls >> /tmp/ls.txt   這個格式的前一部分是對時間的設定, 後面一部分是要執行的命令,如果要執行的命令太多,可以把這些命令寫到一個腳本里面,然後在這裡直接呼叫這個指令碼就可以了,呼叫的時候記得寫出命令的完整 路徑。時間的設定我們有一定的約定,前面五個*號代表五個數字,數字的取值範圍和含義如下:   分鐘 (0-59) 小時(0-23) 日期(1-31) 月份(1-12) 星期(0-6) //0代表星期天       除了數字還有幾個個特殊的符號就是"*"、"/"和"-"、",",*代表所有的取值範圍內的數字,"/"代表每的意思,"*/5"表示每5個單位,"-"代表從某個數字到某個數字,","分開幾個離散的數字。以下舉幾個例子說明問題:   每天早上6點 ----------------- 0 6 * * * echo "Good morning." >> /tmp/test.txt //注意單純echo,從螢幕上看不到任何輸出,因為cron把任何輸出都email到root的信箱了。   每兩個小時 ----------------- 0 */2 * * * echo "Have a break now." >> /tmp/test.txt   晚上11點到早上8點之間每兩個小時,早上八點 ----------------- 0 23-7/28 * * * echo "Have a good dream:)" >> /tmp/test.txt   每個月的4號和每個禮拜的禮拜一到禮拜三的早上11點 ----------------- 0 11 4 * 1-3 command line   1月1日早上4點 ----------------- 0 4 1 1 * command line       每 次編輯完某個使用者的cron設定後,cron自動在/var/spool/cron下生成一個與此使用者同名的檔案,此使用者的cron資訊都記錄在這個檔案 中,這個檔案是不可以直接編輯的,只可以用crontab -e 來編輯。cron啟動後每過一份鍾讀一次這個檔案,檢查是否要執行裡面的命令。因此此檔案修改後不需要重新啟動cron服務。   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內的指令碼