How to end the process

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

How do I end this in another script. I am unable to pass the resource returned by proc_open.

I also tried using proc_get_status (), it returns ppid. I am not getting pid of children.

development env: WAMP

Any inputs are appreciated.

+2


source to share


4 answers


I recommend that you revisit your model to make sure you really need to kill the process from somewhere else. Your code will become increasingly difficult to debug and maintain in all but the most trivial circumstances.

To keep it encapsulated, you can signal the process you want to terminate and gracefully exit in the process you want to kill. Otherwise, you can use regular IPC to send a message that says, "Hey buddy, please turn it off."



edit: For the second paragraph, you can still run the script to do this. it's great. what you want to avoid is something like kill -9. instead, let the process exit gracefully.

+3


source


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



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

      

+3


source


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

Does this work for you?

$process = proc_open('php', $descriptorspec, $pipes, $cwd, $env);
$return_value = proc_close($process);

      

0


source


Your best bet is to use something like this to start another process:

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

      

For this process to execute and give you the current process id.

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

      

to quit you can always use

exec("kill $pid");

      

However, you cannot kill processes that do not belong to the user running PHP - if it runs like nobody else - you start a new process like nobody else, and you can only kill processes that run under the user.

0


source







All Articles