MirroredFate
MirroredFate

Reputation: 12816

Running a Laravel Artisan command as a new process

So in general you can say exec('MyCommand &> /dev/null &') in php and MyCommand will be executed as a seperate process, so your primary php execution can continue on it's merry way.

Oddly, if you try to do this using Laravel's Artisan, something goes sideways. For instance, exec('php artisan command &> /dev/null &') results, surprisingly, in the process still just hanging until the artisan command finishes. If you wrap the artisan command in a bash script, it won't execute at all.

Why is this, and how can I execute an artisan command in a new, detached process?

Upvotes: 0

Views: 4324

Answers (1)

Antonio Carlos Ribeiro
Antonio Carlos Ribeiro

Reputation: 87719

You'll have to create a new process to run it:

$pid = pcntl_fork();

switch($pid)
{
    case -1:    // pcntl_fork() failed
        die('could not fork');

    case 0:    // you're in the new (child) process
        exec("php artisan command");

        // controll goes further down ONLY if exec() fails
        echo 'exec() failed';

    default:  // you're in the main (parent) process in which the script is running
        echo "hello";
}

Upvotes: 1

Related Questions