Shashidhar Gr
Shashidhar Gr

Reputation: 418

php background process in windows environment

am using windows-7 OS and wamp server. i have 2 php files trigger.php,background.php. i want to run background.php in background .i have to call this file from trigger.php .

i tried below methods.

i added this code in trigger.php

    $WshShell = new COM("WScript.Shell");
   $oExec = $WshShell->Run("C:\wamp\bin\php\php5.3.5\php-win.exe -f C:/wamp/www/background.php", 0, false);

but my background.php is not getting called.

how i can do this?

any suggestions are appreciated.

Upvotes: 4

Views: 10207

Answers (3)

DEV Tiago França
DEV Tiago França

Reputation: 1716

Update:


function execInBackground(string $cmd)
{
    if (substr(php_uname(), 0, 7) == "Windows") {
        pclose(popen("start /B {$cmd}", "r"));
        return;
    }

    exec("{$cmd} > /dev/null &");
}

$phpBinPath = defined('PHP_BINARY') ? PHP_BINARY : '/usr/bin/php';
$fileToRun = __DIR__ . '/sleep.php';
$cmd = "'{$phpBinPath}' '{$fileToRun}'";

execInBackground($cmd);

echo 'Executou!!!';

Upvotes: 0

Shashidhar Gr
Shashidhar Gr

Reputation: 418

i changed the function as below,

$cmd = 'C:\wamp\bin\php\php5.3.5\php.exe C:\wamp\www\email3.php';

function execInBackground($cmd) { 
    if (substr(php_uname(), 0, 7) == "Windows"){ 
        pclose(popen("start /B " . $cmd, "r"));  
    } 
    else { 
        exec($cmd . " > /dev/null &");   
    } 
}

and , its works for me. :)

Upvotes: 1

Kris
Kris

Reputation: 8873

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"

Upvotes: 17

Related Questions