Execute Laravel/Symfony/Artisan Command in Background

12,301

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();
Share:
12,301
ExoticChimp
Author by

ExoticChimp

Updated on June 14, 2022

Comments

  • ExoticChimp
    ExoticChimp almost 2 years

    I need to execute a Laravel long running process in the background for consuming the Twitter Streaming API. Effectively the php artisan CLI command I need to run is

    nohup php artisan startStreaming > /dev/null 2>&1 &
    

    If I run that myself in the command line it works perfectly.

    The idea is that I can click a button on the website which kicks off the stream by executing the long running artisan command which starts streaming (needs to run in the background because the Twitter Streaming connection is never ending). Going via the command line works fine.

    Calling the command programatically however doesn't work. I've tried calling it silently via callSilent() from another command as well as trying to use Symfony\Component\Process\Process to run the artisan command or run a shell script which runs the above command but I can't figure it out.

    Update If I queue the command which opens the stream connection, it results in a Process Timeout for the queue worker

    I effectively need a way to run the above command from a PHP class/script but where the PHP script does not wait for the completion/output of that command.

    Help much appreciated

  • ExoticChimp
    ExoticChimp over 8 years
    Running Artisan queue does successfully run the command, however I it blocks that queue worker. Because the process the Artisan command runs never actually finishes (the Twitter streaming API is a persistent never ending connection). Eventually, the process will timeout
  • Jeemusu
    Jeemusu over 8 years
    Probably should of stated that the process runs indefinitely, rather than using the term "long running" in your original question.
  • ExoticChimp
    ExoticChimp over 8 years
    queuing isn't required as the issue was with the command not being executed in the background which would cause the queue worker to timeout when executing the job. using the Symphony Process component enabled me to run the command in the background
  • Raghavendra N
    Raghavendra N over 8 years
    @Jeemusu can you plz tell me what is the 'path/to/artisan' here? I've given the path to laravel project installation directory as the 2nd parameter? I'm getting this error message: Could not open input file: artisan
  • Kashif Naseem
    Kashif Naseem about 4 years
    @Jeemusu you can use base_path() link
  • Yevgeniy Afanasyev
    Yevgeniy Afanasyev almost 2 years
    You should not put big tasks to a queue, because 'retry_after' parameter cannot be set per job.
  • Yevgeniy Afanasyev
    Yevgeniy Afanasyev almost 2 years
    Argument 1 passed to Symfony\Component\Process\Process::__construct() must be of the type array, string given