Execute Laravel/Symfony/Artisan Command in Background
The Symfony Process Component by default will execute the supplied command within the current working directory, getcwd()
.
The value returned by getcwd()
will not be the Laravel install directory (the directory that contains artisan), so the command will most likely returning a artisan: command not found
message.
It isn't mentioned in the Process Component documentation but if you take a look at the class file, we can see that the construct allows us to provide a directory as the second parameter.
public function __construct(
$commandline,
$cwd = null,
array $env = null,
$input = null,
$timeout = 60, array
$options = array())
You could execute your desired command asynchronously by supplying the second parameter when you initialise the class:
use Symfony\Component\Process\Process;
$process = new Process('php artisan startStreaming > /dev/null 2>&1 &', 'path/to/artisan');
$process->start();