2

I am trying to kill a process with pid 38456 using Symfony with this code:

$process1 = new Process('kill -9 38456');
$process1->run();

Pitifully this doesn't work. I think this is due to permissions (Symfony only can kill its own process) but I am not sure about it.

luchaninov
  • 6,792
  • 6
  • 60
  • 75
amarincolas
  • 141
  • 14
  • 2
    Do you get any output from the process on stdout or stderr, or a return code from the command (i.e. `$process1->getErrorOutput()` or `$process1->getExitCode()` )? – wonderb0lt May 27 '15 at 12:49
  • Using `$process1->getErrorOutput()` prints sh: line 0: kill: (38456) - Operation not permitted and using `$process1->getExitCode()` prints 1. – amarincolas May 28 '15 at 05:29
  • 1
    That message is pretty self-explanatory. You have to consider getting privileges to kill that process (via sudo, or modifying the owner of your Symfony process). – wonderb0lt May 28 '15 at 08:16

1 Answers1

1

Try to understand which user runs your code:

$process = new Process('whoami');
$process->run();
echo $process->getOutput();

If you are apache or www-data then most probably you have very limited rights.

Your options:

  • run your script from CLI (command-line) as root. This is the way I recommend
  • run the process you want to kill from your process
  • run web-server (apache or php-fpm) as root - very insecure
  • strange way - web-server can kill process not directly but for example create some file with process id to kill. Also there will be CLI process running by rout that will read this file and kill that process.
luchaninov
  • 6,792
  • 6
  • 60
  • 75