php實現定時任務的兩種方法
1、利用寶塔面板簡單設置定時任務
1,在對應的服務器上安裝寶塔。
2,打開寶塔設置計劃任務。
設置定時任務的方法很多,這是我覺得最簡單的設置定時任務的方法。
2、在Crontab中使用URL執行腳本
如果你的PHP腳本可以通過URL觸發,你可以使用 lynx 或 curl 或 wget 來配置你的Crontab。
1、下面的例子是使用Lynx文本瀏覽器訪問URL來每小時執行PHP腳本。Lynx文本瀏覽器默認使用對話方式打開URL。但是,像下面的,我們在lynx命令行中使用-dump選項來把URL的輸出轉換來標準輸出。
00 * * * * lynx -dump http://www.baidu.com/index/getlist
2、下面的例子是使用 CURL 訪問URL來每5分執行PHP腳本。Curl默認在標準輸出顯示輸出。使用 “curl -o” 選項,你也可以把腳本的輸出轉儲到臨時文件temp.txt。
*/5 * * * * /usr/bin/curl -o temp.txt http://www.baidu.com/index/getlist
3、下面的例子是使用WGET訪問URL來每10分執行PHP腳本。-q 選項表示安靜模式。"-O temp.txt" 表示輸出會發送到臨時文件。
*/10 * * * * /usr/bin/wget -q -O temp.txt http://www.sf.net/myscript.php
4、訪問URL來執行PHP函數,和2是一樣的,我加了一個pass作爲密碼,防止別人直接訪問我的鏈接,只有pass值和對應的函數裏設定的值相同纔會執行。
00 8 */1 * * /usr/bin/curl http://www.lmyy.com/api.php/auto_run/run_cashout.html?pass=fe10b208819b827c801d4e76056d74d3 > /dev/null
00 19 */1 * * /usr/bin/curl http://www.lmyy.com/api.php/auto_run/run_confirm_order.html?pass=fe10b208819b827c801d4e76056d74d3 > /dev/null
00 19 */1 * * /usr/bin/curl http://www.lmyy.com/api.php/auto_run/run_cancel_order.html?pass=fe10b208819b827c801d4e76056d74d3 > /dev/null
00 */1 * * * /usr/bin/curl http://www.lmyy.com/api.php/auto_run/run_unfreeze_money.html?pass=fe10b208819b827c801d4e76056d74d3 > /dev/null
00 7 */1 * * /usr/bin/curl http://www.lmyy.com/api.php/auto_run/run_refund.html?pass=fe10b208819b827c801d4e76056d74d3 > /dev/null