Discussion:
detaching and running php processes in the background
Gary C. New
2003-11-21 10:40:05 UTC
Permalink
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
Burhan Khalid
2003-11-21 11:25:23 UTC
Permalink
Post by Gary C. New
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?
Try something like :

if (!($handle = popen("script.sh 2>&1 > /dev/null &", "r")))
{
echo "Couldn't run process";
}

http://www.php.net/popen
--
Burhan Khalid
phplist[at]meidomus[dot]com
http://www.meidomus.com
-----------------------
"Documentation is like sex: when it is good,
it is very, very good; and when it is bad,
it is better than nothing."
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Eugene Lee
2003-11-21 11:39:14 UTC
Permalink
On Fri, Nov 21, 2003 at 03:40:05AM -0700, 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?

How about exec('command &') ?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Gary C. New
2004-01-06 10:24:52 UTC
Permalink
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
Post by Gary C. New
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
Continue reading on narkive:
Loading...