3

How can I get PID of process started by Symfony? The code bellow

$process = new \Symfony\Component\Process\Process('vlc');
$process->start();
return $process->getPid();

returns PID 1488. But there is no process (no vlc, no php) in system with same PID.

Edit

Presented code runs in app/console (Symfony\Component\Console\Command\Command)

Community
  • 1
  • 1
Konpaka
  • 322
  • 3
  • 11
  • Have you read this ? http://php-and-symfony.matthiasnoback.nl/2014/03/test-symfony2-commands-using-the-process-component-and-asynchronous-assertions/ – Pedro Lobito Apr 18 '15 at 03:57
  • I dont want to create the file. I need to get directly PID of the started process when it starts. – Konpaka Apr 18 '15 at 04:21

2 Answers2

2

Process forking

It's not unlikely for processes to spawn off their UI separately and letting the starter process end normally, i.e.

----> vlc (1488) ---> EOL
       |
       +--> vlc-ui (??) ---> Application

This behaviour is observable by running the application from the command line and checking whether the prompt returns almost immediately.

Hangup signal

Also note that when a parent process exits (your script), the child process may choose to also exit by listening to SIGHUP signals. If you're not doing so already, you could let your script "live" longer by adding a sleep() statement while you investigate.

Community
  • 1
  • 1
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • I will try with another app. UPD: I ran the ezstream -c /abs_path_to_cfg.xml Same result. – Konpaka Apr 18 '15 at 04:13
  • The Symfony component you're using should already encapsulate that. – Ja͢ck Apr 18 '15 at 05:33
  • Nope. I just watching sources: `$this->process = proc_open($commandline, $descriptors, $this->processPipes->pipes, $this->cwd, $this->env, $this->options);` $commandline is unchanged from constructor. – Konpaka Apr 18 '15 at 05:37
  • @Konpaka [This](https://github.com/symfony/symfony/blob/2.7/src/Symfony/Component/Process/Process.php#L1327) is where `proc_get_status()` is called ... – Ja͢ck Apr 18 '15 at 08:39
  • I forget to check getPid(), my bad. Thank you for reply, I will try to see tomorrow. – Konpaka Apr 18 '15 at 09:32
0

Another approach that may work in some situations is doing it inversely like a script to grep parse a 'ps -A' containing the desired process (PHP e.g.) and rip off PID from the result. You don't have control on "who's who" on the result set but have control on "who's" actually running.