views:

346

answers:

3

Hello ,

I am creating a process using proc_open in one PHP script.

How do i terminate this in another script . I am not able to pass the resource returned by the proc_open.

I also tried using proc_get_status() , it returns the ppid . I don't get the pid of the children .

development env : WAMP

Any inputs is appreciated .

Mithun

A: 

You can use some methond to create process, this method usually returns the PID of the new process.

Does this works for You? :

$process = proc_open('php', $descriptorspec, $pipes, $cwd, $env);
$return_value = proc_close($process);
bua
$process here is a resource not the pid. I cannot/not able to pass this to another script to terminate it .
mithunmo
There is $process.pid, which You could use probably.You could write it to file (not efficient) or send by network to other process ("script").
bua
$process.pid returns "Resource id #6pid ". What do I do with this ?
mithunmo
A: 

You're best off using something like this to launch your other process:

$pid = shell_exec("nohup $Command > /dev/null 2>&1 & echo $!");

That there would execute the process, and give you a running process ID.

exec("ps $pid", $pState);     
$running = (count($pState) >= 2);

to terminate you can always use

exec("kill $pid");

However, you cant kill processes not owned by the user PHP runs at - if it runs as nobody - you'll start the new process as nobody, and only be able to kill processes running under the user nobody.

Sauron
No I need to use proc_open as I need to send lot of commands in one instance.
mithunmo
+1  A: 

I recommend that you re-examine your model to make certain that you actually have to kill the process from somewhere else. Your code will get increasingly difficult to debug and maintain in all but the most trivial circumstances.

To keep it encapsulated, you can signal the process you wish to terminate and gracefully exit in the process you want to kill. Otherwise, you can use normal IPC to send a message that says: "hey, buddy. shut down, please."

edit: for the 2nd paragraph, you may still end up launching a script to do this. that's fine. what you want to avoid is a kill -9 type of thing. instead, let the process exit gracefully.

San Jacinto
The thing is I have a html page with start and stop buttons. The start goes to a script called "start.php" where it creates a new processs. The stop button needs to stop/terminate the process. How do I do this ? As far as I can think the stop functionality needs to be written in separate script right ?
mithunmo
I don't think so. You have scession. You have access to cookies. you have access to the file system where you can write a file, etc (actually, that last one you might not, depending upon security setting). You can save the pid of the spanwed process and recall it later and use the techniques i mentioned above. I'm sorry I can't give you exact syntax; I'm forever looking up that type of stuff myself.
San Jacinto
actually, if it's all just static html except for a couple small php scripts, scession is probably out of the question.
San Jacinto
blah.. and it's "session" not "scession." forgive me.
San Jacinto