Some time ago, I was looking for a method to keep a PHP script running when the user aborted. (When the user closed it's browser). I needed this for some long queries to complete even if the user decided to leave.
ignore_user_abort(1);
set_time_limit(0);
while (true) {
// Do queries
}
How would I kill the above script?
Update
The getmypid()
function has been suggested to me. I now implemented the following:
ignore_user_abort(1);
set_time_limit(0);
// Get process id
$pid = getmypid();
$stmt = "INSERT INTO processes (pid, started) VALUES ('".$pid."', NOW())";
while (true) {
// Do Queries
if ($query) {
break;
}
}
$stmt = "UPDATE processes SET ended=NOW() WHERE pid='".$pid."'";
I can now query the database to see hwat processes have not finished yet. I'll know that these processes are stuck.