phplaravellaravel-artisansymfony-process

Unable to run a Laravel Artisan command with parameters using Symfony process


I'm trying to start a process with Symfony in my Laravel app on my shared hosting server that will call an Artisan command with parameters like so:

$process = new Process(['/usr/local/bin/php', base_path('artisan'), 'queue:work --stop-when-empty']);
$process->setTimeout(null);
$process->start();

This does not work as I get:

ERROR: Command "queue:work --stop-when-empty" is not defined.

Did you mean one of these?
    queue:batches-table
    queue:clear
    queue:failed
    queue:failed-table
    queue:flush
    queue:forget
    queue:listen
    queue:monitor
    queue:prune-batches
    queue:prune-failed
    queue:restart
    queue:retry
    queue:retry-batch
    queue:table
    queue:work {"exception":"[object] (Symfony\\Component\\Console\\Exception\\CommandNotFoundException(code: 0): Command \"queue:work --stop-when-empty\" is not defined.

The problem is with the parameter --stop-when-empty as the command runs succesfully wthout it. How can I pass this parameter to the command ?


Solution

  • The process adds quotes around each array value that you pass in. Right now, it's attempting to run

    "exec '/usr/local/bin/php' '/your/path/to/artisan' 'queue:work --stop-when-empty'"
    

    So by adding quotes around the whole string, it's treating 'queue:work --stop-when-empty' as a single command, instead of a command and option. Separate out the command so that it will quote it properly

    $process = new Process(['/usr/local/bin/php', base_path('artisan'), 'queue:work', '--stop-when-empty']);