3

止められない while ループを作成する方法、既に実行されている場合は再度実行しないでください。実行中のスクリプトがデータベースの最後の cron 時刻を更新すると仮定し、スクリプトは現在の時刻 - 40 秒が最後の cron 時刻よりも小さいかどうかを確認します。私が持っているもののサンプルですが、それはオーバーループしています (何度も実行してプロセスを実行します) ありがとう!

<?php
ignore_user_abort(1); // run script in background 
set_time_limit(0);    // set limit to 0


function processing()
{
//some function here
setting::write('cron_last_time', $time); // write cron last time to database
}

$time = current_timestamp();  // current time
$time_before = $time - 20;   //

$settings = setting::read('module_cron_email'); // Get last cron time from database ( in array )
$time_last = $settings['setting_value'];     // Last cron time get value from array



if ($time_before < $time_last) // check if time before is less than last cron time
{ 
   do{
   processing(); //run our function
   sleep(7);    // pause for 7 seconds
   continue;   // continue
   }
   while(true); // loop
}else{

echo "already running";
die();
}
?> 
4

2 に答える 2

6

スクリプトを開始するときに、ロック ファイルを確認します。存在する場合は、ループを終了またはスキップします。存在しない場合は、ファイルを作成して実行します。

于 2012-05-05T16:57:44.230 に答える
-2

PHP PCNTLが必要です。それを使用してPHPプロセスをフォークできます。

                 while(true) { // Infinite

                     $pid = pcntl_fork();

                        switch (pcntl_fork($pid)) {

                                case -1:

                                        die('Fork failed');

                                        break;

                                case 0: // Child

                                        processing();

                                        exit(0);

                                        break;

                                default: // Parent

                                        pcntl_waitpid($pid, $status);

                                        break;

                        }

        }
于 2012-05-05T16:58:10.357 に答える