How to create unstoppable while loop,
if it’s already running dont run it again, I assume that if script running it will update last cron time in database, and script will check if current time – 40 seconds is less than last cron time. Sample of what I’ve, but that’s overlooping ( running again and again do process ) Thanks!
<?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();
}
?>
When starting the script, check for a lock file. If it does exist, quit or skip the loop. If it doesn’t exist, create the file and let it run.