php計劃任務php中的ignore_user_abort實現計劃任務

函數-ignore_user_abort,這個函數可以幫助我們實現像linux中的cron一樣實現計劃任務,下面一起來看下該如何來實現。 

首先看下php手冊對這個函數的解釋

Description
int ignore_user_abort ([ bool $setting ] )
Sets whether a client disconnect should cause a script to be aborted.

也就是說無論客戶端是否關閉瀏覽器,下面的程序都會執行.

再看下其參數

Parameters

setting 
If not set, the function will only return the current setting.

這個函數接受一個參數,來決定是否啓用ignore_user_abort的功能。

再看其返回值:

Return Values
Returns the previous setting, as a boolean.

這裏說返回前一次的設置,並且是bool值得,經過我的測試,這個說法是不對的,返回的明明是int型的,不相信的話大家可以寫一個php文件來測試下。

說了這麼多了,到底該如何用php的這個函數實現計劃任務呢?還得藉助另外一個函數,這個函數是set_time_limit,通過 set_time_limit(0)可以設置程序的執行時間爲無限制,php默認的執行時間是30秒,通過set_time_limit(0)可以讓程序無 限制的執行下去。在程序執行之前加上ignore_user_abort(1)和set_time_limit(0) 即可以了,最終程序該如何寫呢?給大 家一個例子。

  1. ignore_user_abort(); // run script in background  
  2. set_time_limit(0); // run script forever  
  3. $interval=60*15; // do every 15 minutes...  
  4. do{  
  5. // add the script that has to be ran every 15 minutes here  
  6. // ...  
  7. sleep($interval); // wait 15 minutes  
  8. }while(true);  
  9. ?>  

發佈了66 篇原創文章 · 獲贊 42 · 訪問量 94萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章