In order to get this to work I had to do several things.

1. I had initially compiled my php installation as a server-side module, but in order to execute and detach a background php process I had to recompile my php installation as a binary installation (hint: if you compile php as a binary and then as a server-side module you can take advantage of both installation types--that is what I ended up doing).

2. Next you must reference the php binary with the '-f' option to execute the desired php script. To pass variables, you must reference them in a single quoted string prefacing each with a '&' symbol. Finally write all signals to '/dev/null' and release it into the background using an ending '&' symbol.

Example:

shell_exec("/usr/local/bin/php -f ./user-create.php '&uid=" . $uid . "&vdomain=" . $vdomain . "&password=" . $password . "' > /dev/null &");

3. In the primary php script (./user-create.php) escape any shell command variables that have been supplied by human input (security).

$u = escapeshellcmd($uid);
$d = escapeshellcmd($vdomain);
$p = escapeshellcmd($password);

4. You should put any child commands you want to spawn in the primary script (./user-create.php) as this will help with maintaining the number of rogue processes that you might need to deal with.

shell_exec("useradd -d /home/$d $u");
shell_exec("passwd $u && $p");

That's it!

I know it sounds a bit complicated, but after you have all the right components functional it is quite easy.

Hope this helps all those of you who have requested it.

Respectfully,


Gary



Gary C. New wrote:
What is the best way to detach and run a command line based php script in the background from a web-based php script, without the need for reporting?

I have a web-based php script that gathers information and then passes it off to several functions, which execute several other php scripts. The problem is that the main php script does not return until all the processes have signaled and is taking about 30 sec to complete. There is no need for the child processes spawned by the main php script to signal. I would like to spawn and detach them to finish in the background; thus, dramatically reducing the load time of the main php script.

It has been suggested to me to use the & with the shell_exec function, but this still requires the child processes to signal.

Another suggestion was to use the pcntl_fork function, but my reading suggest that this is not to be used with web-based applications and does not compile in with mod_php (only the binary version). If I were to use the binary version would I even be able to detach the forked child processes? Doesn't pcntl_fork still require the child processes to signal to the parent before exiting otherwise becoming zombie processes?

Any suggestions on the best way to handle this?

Respectfully,


Gary



-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to