I’m trying to fork a process by getting PHP to exec PHP whilst piping output to /dev/null and running the cmd in the background
Before I get this running silently – I want to check the cmd runs as expected – however when the command is executed by PHP the process (both master and forked) never terminate and the browser is forver stuck on “Connecting…”
Here is the code:
$script = '/home/username/application/bg_job.php';
$args = 'foo bar';
$cmd = "php -f $script $args";
$return = exec($cmd, $arr_output, $code);
var_dump($return);
I checked the Apache user which runs the script by doing
whoami
and it was “username” – so I’ve logged in as root and run
sudo -u username php -f /home/username/application/bg_job.php foo bar
And it works.
Any ideas what I might be doing wrong?
EDIT – FYI – here is the contents of bg_job.php
<?php
file_put_contents('/home/username/test.txt', 'Hello !');
?>
I think you are attempting to instigate a background task. On Linux this can be done with the ampersand:
There are more details on this blog post.
So your command would become: