Asked  7 Months ago    Answers:  5   Viewed   43 times

I'm trying to setup a php trigger file that will set off a background process. (see this question)

I'm doing this on a Windows Wampserver environment.

So for example I have trigger.php that runs the exec function that calls for my backgroundProcess.php to be parsed and executed.

However the problem is that my trigger.php file is waiting for the exec() command to finish running backgroundProcess.php before it stops. The background process runs for about 20-30 seconds, and trigger.php is waiting all that time until backgroundProcess.php has fully finished.

Is that making sense? Here is the trigger.php file that runs the exec() command

exec('C:wampbinphpphp'.phpversion().'php.exe -f C:pathtobackgroundProcess.php > C:wampbinphpphp'.phpversion().'devnull &');

Basically, I'm wanting trigger.php to just trigger off the backgroundProcess and not wait around for it to finish.


EDIT

Problem solved with the following command:

$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run("C:wampbinphpphpVERSIONNUMBERphp-win.exe -f C:/wamp/www/path/to/backgroundProcess.php", 0, false);

 Answers

100

Problem solved with the following command:

$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run("C:wampbinphpphpVERSIONNUMBERphp-win.exe -f C:/wamp/www/path/to/backgroundProcess.php", 0, false);
Wednesday, March 31, 2021
 
AntoineB
answered 7 Months ago
94

Assuming this is running on a Linux machine, I've always handled it like this:

exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd, $outputfile, $pidfile));

This launches the command $cmd, redirects the command output to $outputfile, and writes the process id to $pidfile.

That lets you easily monitor what the process is doing and if it's still running.

function isRunning($pid){
    try{
        $result = shell_exec(sprintf("ps %d", $pid));
        if( count(preg_split("/n/", $result)) > 2){
            return true;
        }
    }catch(Exception $e){}

    return false;
}
Tuesday, June 1, 2021
 
FyodorX
answered 5 Months ago
60

exec() will block until the process you're exec'ing has completed - in otherwords, you're basically running your 'test.php' as a subroutine. At bare minimum you need to add a & to the command line arguments, which would put that exec()'d process into the background:

exec("php test.php {$test['id']} &");
Saturday, June 19, 2021
 
motanelu
answered 5 Months ago
90

You should investigate the pcntl_fork and related functions. This allows one master process to form a number of child processes and be notified of child exit status.

http://php.net/manual/en/function.pcntl-fork.php

Saturday, August 7, 2021
 
Elijah W. Gagne
answered 3 Months ago
23
function execInBackground($cmd) { 
    if (substr(php_uname(), 0, 7) == "Windows"){ 
        pclose(popen("start /B ". $cmd, "r"));  
    } 
    else { 
        exec($cmd . " > /dev/null &");   
    } 
}

in this case your $cmd would be "php C:/wamp/www/path/to/background.php"

Friday, August 13, 2021
 
user542584
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :