Monitoring Children Forked using PHP Monitoring Children Forked using PHP php php

Monitoring Children Forked using PHP


You can definitely monitor the child process:

$pid = pcntl_fork();if ($pid == -1) { die('could not fork');} else if ($pid) { // we are the parent pcntl_waitpid($pid, $status, WUNTRACED); //Protect against Zombie children if (pcntl_wifexited($status)) {   echo "Child exited normally"; } else if (pcntl_wifstopped($status)) {   echo "Signal: ", pcntl_wstopsig($status), " caused this child to stop."; } else if (pcntl_wifsignaled($status)) {   echo "Signal: ",pcntl_wtermsig($status)," caused this child to exit with return code: ", pcntl_wexitstatus($status); }} else { pcntl_exec("/path/to/php/script"); echo "Could not Execute...";}
  • pcntl_wifexited() - Checks if status code represents a normal exit
  • pcntl_wifstopped() - Checks whether the child process is currently stopped
  • pcntl_wifsignaled() - Checks whether the status code represents a termination due to a signal
  • pcntl_wexitstatus() - Returns the return code of a terminated child
  • pcntl_wtermsig() - Returns the signal which caused the child to terminate
  • pcntl_wstopsig() - Returns the signal which caused the child to stop

EDIT:

To clarify regarding messaging between parent and child process; you definitely cannot catch Exceptions across processes. As far as messaging, using only the PCNTL library you are also limited to process signals and exit codes.

Not knowing what, exactly, you are doing. You have a variety of other options. I'd suggest one of the following asynchronous messaging solutions, as they could possibly cover your needs.

File based

Your child processes could write messages to a file, which would be polled by the parent.

Memcache based

Same as above, but using memcached as the communications medium.

Database based

Same as above, but using a db table as the communications medium.

PHP's semaphore/IPC library

http://us3.php.net/manual/en/book.sem.php

This allows you to use methods like msg_send() and msg_receive() to communicate across processes.

I'm confident one of these will provide the solution you are looking for. Going into the specifics of how to use any of these methods, is probably beyond the scope of this question though, but feel free to ask a new question if you need help with whichever method you choose to utilize.