So I've had some great luck using pcntl and posix functions in command-line scripts. I thought recently "gee wouldn't it be great if you could pcntl_fork off a process from a script hosted by apache to maybe perform that super-long process like converting a video file."
So I concocted a test script and ran it via cli a couple of times but when I tried to access it via apache, this error happens:
And looking in /etc/php5/apache2/php.ini, I see this:
throw new Exception("This script requires posix extension");
It seems odd to me that one would define disable_functions to exclude pcntl functions when this module does not appear to be even installed/loaded. Also, I can't seem to find anything that would explicitly load pcntl functions in the cli php.ini file. Pretty weird.
; This directive allows you to disable certain functions for security reasons.
; It receives a comma-delimited list of function names. This directive is
; *NOT* affected by whether Safe Mode is turned On or Off.
disable_functions = pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,
Anyways, the big question is it kosher to utilize pcntl_fork and related functions when running a PHP script hosted by apache? Obviously the ubuntu package managers didn't want it running. I found some mostly confused/confusing discussion about how to get it running but also some suggestions that this may not be a good idea. I saw another suggestion to use ignore_user_abort but that doesn't sound like a good idea to me -- I wouldn't want my pool of PHP processes available to apache to get used up processing video or calculating pi to the millionth decimal only to starve apache of these processes.
I have managed in the past to fork off a CLI PHP process from apache like so:
and then utilized pcntl_fork and/or posix_setsid to liberate and divorce the cli process from its cruel apache master.
$cmd = "/usr/bin/php /path/to/some/script.php [parameter] > /dev/null & echo \$!";
$cmd_output = NULL; // will contain an array of output lines from the exec command
$cmd_result = NULL; // will contain the success/failure code returned by the OS. Will be zero for a valid command, non-zero otherwise
$cmd_return = exec ( $cmd, $cmd_output, $cmd_result ); // $return will contain the last line from the result of the command which should be the PID of the process we have spawned
But that seems like a lot of work to me. I believe I had to do it this way a) because apache doesn't have pcntl enabled and b) just calling exec is not enough because the exec'ed process can get killed when the browser disconnects or a timeout happens or whatever.