0

I would like to run a background process, which is orphaned, with Symfony Process component. I can't use kernel.terminate event. This is what I have tried to do:

This is process I would like to run:

// script.php
$f = fopen('test'.uniqid().'.txt', 'w');
for ($i = 0; $i < 1000; $i++) {
    fwrite($f, "Line $i \n");
    sleep(2);
}
fclose ($f);

And this is script which I would like to run my process:

// test.php
$command = "php script.php > /dev/null 2>&1 &";

$p = new \Symfony\Component\Process\Process($command);
$p->start();
echo $p->getPid()."\n";

The problem is that after test.php termination, script.php is not working. Termination of main script kills all subprocesses. Is it possible to have orphaned processes running with Symfony Process?

1
  • 1
    What are you trying to achieve first before I answer this Commented Apr 5, 2017 at 15:38

2 Answers 2

1

Easiest way, don't use the Process Component and fall back to php exec:

http://php.net/manual/de/function.exec.php

exec('php script.php > /dev/null 2>&1 &');

For a more solid solution consider something like a schedule and a crontab executed command to start the process when one needs to be started.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm fairly certain this will do the same thing as the OP is already doing.
It is not. Process component uses proc_open which actually returns a resource for the output which is monitored by the Process component. exec works different it's fire and forget (given the last & at the end of the executed command)
Process lib is bloated and useless. Lost a lot of time playing with it and ended up with old good exec().
0

You can use at (atd) or cron with a custom queue. PHP invokes synchronous command which enqueues the desired command for later execution. The at is quite powerful and standard component of unix systems. You simply run at now + 10 min and push commands to its stdin, then the atd daemon will run your commands in 10 minutes.

If you wish to run your command directly from PHP, try to add shell: new Process('/bin/sh -c "php script.php &"'). I'm not sure how exactly Process class invokes the command, but if & does not work, one more shell in between will do the trick (that is what exec() does).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.