Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Killing child process started by pcntl_fork

Tags:

linux

php

posix

I am using pcntl_fork to start a child process to send an email via SMTP.

The child process uses the PEAR Mail package to send the email, but the trouble is if the remote server doesn't respond the process just runs forever waiting for a response, regardless of any time limit that is set in php.ini.

To get around this I'm using pcntl_alarm function to trigger a function after 30 seconds that kills the child process if it's still running.

function handlesig($sig) {
    global $pid,$node,$resend;
    posix_kill($pid,SIGKILL);
    mysql_query("insert into log (event) values ('Timed out!')");
}

When I kill the child process though I'm left with a defunct process on the system.

Is there a different signal I should use that will still force the child process to die without waiting for the connection (because the connection will never finish) and avoid a build up of defunct processes?

like image 594
Tim Avatar asked Nov 12 '09 09:11

Tim


1 Answers

You need either wait for it as described above, or make the child process to detach from the parent using posix_setsid

like image 130
StasM Avatar answered Oct 09 '22 04:10

StasM