Vidya
Vidya

Reputation: 8197

How to terminate a process

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 .

Upvotes: 2

Views: 18629

Answers (4)

laurent
laurent

Reputation: 90736

To do that in pure PHP, here is the solution:

posix_kill($pid, 15); // SIGTERM = 15

Upvotes: 3

user124493
user124493

Reputation:

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.

Upvotes: 3

Sauron
Sauron

Reputation: 16903

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.

Upvotes: 1

bua
bua

Reputation: 4870

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);

Upvotes: 0

Related Questions