0

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?

MirroredFate
  • 12,396
  • 14
  • 68
  • 100

1 Answers1

1

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";
}
Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
  • Sadly, I don't think I can use `pcntl_fork()` from an Apache module call, as per [this](http://stackoverflow.com/questions/16826530/pcntl-fork-returning-fatal-error-call-to-undefined-function-pcntl-fork) question. – MirroredFate Sep 25 '14 at 18:36
  • Are you sure it's not just because the pcntl_* functions are disabled in your php.ini file? – Antonio Carlos Ribeiro Sep 25 '14 at 18:45
  • They were disabled, and I enabled them out of curiosity- however, I then found the first comment on [this](http://stackoverflow.com/a/14737821/771665) answer, which pretty explicitly states those function are disabled in the Apache config because they don't work. – MirroredFate Sep 25 '14 at 23:29