Syed
Syed

Reputation: 931

shell_exec() should not wait for called function to finish executuion

I have a php file "test.php" that contains following two functions

function scheduler($id, $url) {
    // some code here that need some time to execute
}

function start_scheduler() {
    $id = 10;
    $url = "example.com";
    shell_exec( scheduler($id, $url) . "> /dev/null 2>/dev/null &" );
    echo "Scheduler started";
    exit();
}

Unfortunately, shell_exec() is not functioning what I expect. I want when start_scheduler() is executed it should not wait for scheduler($id, $url) to finish execution rather echo "Scheduler started" and exit.

Upvotes: 0

Views: 71

Answers (1)

sg-
sg-

Reputation: 2176

You are implying that PHP is multi-threaded. It isn't. You can only run one thread which means every line has to wait for the one before it to finish.

If you need to run something asynchronously, the best option is generally to use a cron job so you don't tie up an Apache thread. That is, create a job that calls that PHP script via the command line so it's non-interactive.

Upvotes: 1

Related Questions