PHP中如何實現crontab代碼
PHP是一種通用開源腳本語言。語法吸收了C語言、Java和Perl的特點,利於學習,使用廣泛,主要適用於Web開發領域。下面,小編爲大家搜索整理了PHP中如何實現crontab代碼,希望能給大家帶來幫助!更多精彩內容請及時關注我們應屆畢業生考試網!
1. 準備一個標準crontab文件 ./crontab
代碼如下:
# m h dom mon dow command
* * * * * date > /tmp/
2. crontab -e 將此腳本加入系統cron
代碼如下:
* * * * * /usr/bin/php
3. 源碼
代碼如下:
// 從./crontab讀取cron項,也可以從其他持久存儲(mysql、redis)讀取
$crontab = file('./crontab');
$now = $_SERVER['REQUEST_TIME'];
foreach ( $crontab as $cron ) {
$slices = preg_split("/[s]+/", $cron, 6);
if( count($slices) !== 6 ) continue;
$cmd = array_pop($slices);
$cron_time = implode(' ', $slices);
$next_time = Crontab::parse($cron_time, $now);
if ( $next_time !== $now ) continue;
$pid = pcntl_fork();
if ($pid == -1) {
die('could not fork');
} else if ($pid) {
// we are the parent
pcntl_wait($status, WNOHANG); //Protect against Zombie children
} else {
// we are the child
`$cmd`;
exit;
}
}
/* */
class Crontab {
/**
* Finds next execution time(stamp) parsin crontab syntax,
* after given starting timestamp (or current time if ommited)
*
* @param string $_cron_string:
*
* 0 1 2 3 4
* * * * * *
* - - - - -
* | | | | |
* | | | | +----- day of week (0 - 6) (Sunday=0)
* | | | +------- month (1 - 12)
* | | +--------- day of month (1 - 31)
* | +----------- hour (0 - 23)
* +------------- min (0 - 59)
* @param int $_after_timestamp timestamp [default=current timestamp]
* @return int unix timestamp - next execution time will be greater
* than given timestamp (defaults to the current timestamp)
* @throws InvalidArgumentException
*/
public static function parse($_cron_string,$_after_timestamp=null)
{
if(!preg_match('/^((*(/[0-9]+)?)|[0-9-,/]+)s+((*(/[0-9]+)?)|[0-9-,/]+)s+((*(/[0-9]+)?)|[0-9-,/]+)s+((*(/[0-9]+)?)|[0-9-,/]+)s+((*(/[0-9]+)?)|[0-9-,/]+)$/i',trim($_cron_string))){
throw new InvalidArgumentException("Invalid cron string: ".$_cron_string);
}
if($_after_timestamp && !is_numeric($_after_timestamp)){
throw new InvalidArgumentException("$_after_timestamp must be a valid unix timestamp ($_after_timestamp given)");
}
$cron = preg_split("/[s]+/i",trim($_cron_string));
$start = empty($_after_timestamp)?time():$_after_timestamp;
$date = array( 'minutes' =>self::_parseCronNumbers($cron[0],0,59),
'hours' =>self::_parseCronNumbers($cron[1],0,23),
'dom' =>self::_parseCronNumbers($cron[2],1,31),
'month' =>self::_parseCronNumbers($cron[3],1,12),
'dow' =>self::_parseCronNumbers($cron[4],0,6),
);
// limited to time()+366 - no need to check more than 1year ahead
for($i=0;$i<=60*60*24*366;$i+=60){
if( in_array(intval(date('j',$start+$i)),$date['dom']) &&
in_array(intval(date('n',$start+$i)),$date['month']) &&
in_array(intval(date('w',$start+$i)),$date['dow']) &&
in_array(intval(date('G',$start+$i)),$date['hours']) &&
in_array(intval(date('i',$start+$i)),$date['minutes'])
){
return $start+$i;
}
}
return null;
}
/**
* get a single cron style notation and parse it into numeric value
*
* @param string $s cron string element
* @param int $min minimum possible value
* @param int $max maximum possible value
* @return int parsed number
*/
protected static function _parseCronNumbers($s,$min,$max)
{
$result = array();
$v = explode(',',$s);
foreach($v as $vv){
$vvv = explode('/',$vv);
$step = empty($vvv[1])?1:$vvv[1];
$vvvv = explode('-',$vvv[0]);
$_min = count($vvvv)==2?$vvvv[0]:($vvv[0]=='*'?$min:$vvv[0]);
$_max = count($vvvv)==2?$vvvv[1]:($vvv[0]=='*'?$max:$vvv[0]);
for($i=$_min;$i<=$_max;$i+=$step){
$result[$i]=intval($i);
}
}
ksort($result);
return $result;
}
}
相關文章
-
如何用Photoshop CC中的Camera Raw來進行後期修圖
自從Photoshop CC中置入了Camea Raw後,我們就可以用Photoshop CC對各種格式的圖片進行後期處理,而不再僅限於Raw格式的圖片。事實上,我們可以將Camera Raw當做一個智能濾鏡來用,可以用來調整曝光、銳化、降噪、局部調整和 -
php和javascript之間變量的傳遞實現代碼
本例是PHP和javascript交互的例子,php中的值賦給js變量中,前提是這個php變量必須有值才行,就算是假分支中。比如php中的$flags在本例中爲true,如果js中false語句寫成:$title_rHTML = "";就會出錯,因爲$title在php中被賦值爲 -
php的curl實現get和post的代碼
類似於dreamhost這類主機服務商,是顯示fopen的使用的。使用php的curl可以實現支持FTP、FTPS、HTTP HTPPS SCP SFTP TFTP TELNET DICT FILE和LDAP。具體使用如下,更多消息請關注應屆畢業生網! curl 支持SSL證書、H -
The relationship between theory and practice大學英語作文
We all know that books are a source of there are those who think what the book contains must be is stupid to follow the books without any the old saying goes,to believe everything in books is worse -
如何在PHP中處理Protocol Buffers數據
Protocol Buffers是Google定義的一種跨語言、跨平臺、可擴展的數據傳輸及存儲的協議,因爲將字段協議分別放在傳輸兩端,傳輸數據中只包含數據本身,不需要包含字段說明,所以傳輸數據量小,解析效率高。如何在PHP中處理Protoco -
java使用ConcurrentHashMap和計數器實現鎖的方法
java如何使用ConcurrentHashMap和計數器實現鎖?下面是由本站小編爲大家整理的java使用ConcurrentHashMap和計數器實現鎖的方法,喜歡的可以收藏一下!瞭解更多詳情資訊,請關注應屆畢業生考試網! java 使用ConcurrentHashM -
The Best Thing That Could Ever Happen少兒雙語閱讀
Unit 3 The Best Thing That Could Ever Happen單元 3 最棒的事Three old men were sitting around and talking.三個老頭坐在一起聊天。The 80-year-old remarked, "The best thing that could happen to me would j -
如何在PHP中處理Protocol Buffers數據大綱
我們知道Protocol Buffers是Google定義的一種跨語言、跨平臺、可擴展的數據傳輸及存儲的協議,因爲將字段協議分別放在傳輸兩端,傳輸數據中只包含數據本身,不需要包含字段說明,所以傳輸數據量小,解析效率高。感興趣的可以訪 -
PHP如何實現Unicode和Utf-8編碼相互轉換
通過PHP實現一個函數可以對字符串進行Unicode的編碼和解碼,需要的朋友可以參考下。最近恰好要用到unicode編碼的轉換,就去查了一下php的庫函數,居然沒找到一個函數可以對字符串進行Unicode的編碼和解碼!也罷,找不到的話就 -
有關JavaScript中的prototype.bind()方法介紹
網絡教育已經成爲實現終身教育、終身學習型社會必不可缺的一種教育模式。那麼你要怎麼去寫網絡教育大專自我鑑定呢?下面由本小編精心整理的網絡教育大專自我鑑定範文,希望可以幫到你哦!網絡教育大專自我鑑定範文篇一我