Bluehost.com Web Hosting $6.95

detaching and running php processes in the background

This is a discussion on detaching and running php processes in the background within the PHP General forums, part of the PHP Programming Forums category; What is the best way to detach and run a command line based php script in the background from a ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-21-2003
Gary C. New
 
Posts: n/a
Default detaching and running php processes in the background

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
Reply With Quote
  #2 (permalink)  
Old 11-21-2003
Burhan Khalid
 
Posts: n/a
Default Re: [PHP] detaching and running php processes in the background

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?


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."
Reply With Quote
  #3 (permalink)  
Old 11-21-2003
Eugene Lee
 
Posts: n/a
Default Re: [PHP] detaching and running php processes in the background

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 &') ?

Reply With Quote
  #4 (permalink)  
Old 01-06-2004
Gary C. New
 
Posts: n/a
Default Re: detaching and running php processes in the background

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
>

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 05:22 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0