PHP Exec() as Background Process (Windows Wampserver Environment)

PHP exec() as Background Process (Windows Wampserver Environment)

Problem solved with the following command:

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

php exec in the background with WAMP on Windows

EDIT: Due to the way the exec() command waits for the program to finish executing, it's very difficult to do this with vanilla exec(). I came across these solutions, and this one should work:

$rshell = new COM("WScript.Shell");
$rexec = $rshell->Run("php -f C:/wamp/www/np/myphpscript.php ".$var1, 0, false);

The WScript.Shell->Run command takes 3 arguments: the command (you can optionally add output redirection), window mode (0 = hidden), and wait it should wait to finish. Because the 3rd argument is false, this PHP should return immediately.

Original Solution: As this post suggests, you should try START /B cmd. It is virtually the Linux equivalent of cmd & in that it runs the command asynchronously, in the background, without user interaction or opening a new shell.

Because this will return immediately, PHP won't wait for it to finish, and the exec() command will not receive any output. Instead, try using shell output redirection. Your PHP given code would look like this:

$cmd = 'start /b "" php -f C:/wamp/www/np/myphpscript.php '.$var1.' >C:/wamp/www/np/output.txt';
exec($cmd);

Run a php script as a background process in wamp server

  1. create a batch file to run your php script using php executable "C:\wamp\php\php.exe C:\wamp\www\index.php"
  2. add this batch file in Scheduled Task in Windows control panel.

How to run a background process by PHP in windows?

I checked PHP: popen - Manual and see there a is not a valid mode, but I see this on several answers around here!

however:

The mode. Either 'r' for reading, or 'w' for writing.

By changing mode to r, the script call and run in the background correctly and there is not an error or warning on PHP or Py.

<?PHP
$python = 'C:\\Users\\User\\anaconda3\\python.exe';
$py_script = 'C:\\wamp\\www\\lab\\ex\\simple_test.py';
$py_stdout = '> temp\\'.session_id()."_std.txt";
$py_stderror = '2> temp\\'.session_id()."_stde.txt";
$py_cmd = "$python $py_script $py_arg_1 $py_std $py_stde";

pclose(popen("start /B ". $py_cmd, "r"));

PHP on a windows machine; Start process in background

Will this function from the PHP Manual help?

function runAsynchronously($path,$arguments) {
$WshShell = new COM("WScript.Shell");
$oShellLink = $WshShell->CreateShortcut("temp.lnk");
$oShellLink->TargetPath = $path;
$oShellLink->Arguments = $arguments;
$oShellLink->WorkingDirectory = dirname($path);
$oShellLink->WindowStyle = 1;
$oShellLink->Save();
$oExec = $WshShell->Run("temp.lnk", 7, false);
unset($WshShell,$oShellLink,$oExec);
unlink("temp.lnk");
}

php background process in windows environment

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"

exec() hangs when I run bash command in background

I've managed to fix it, but I have no idea why the new solution works while the old one doesn't. Maybe it has something to do with exec() build-it parser? Both lines work identically in bash so I'm blaming PHP on this.

So, I've replaced

$exec_result = exec('./myapp option1 option2 &> /dev/null &');

with

$exec_result = exec('./myapp option1 option2 > /dev/null 2>&1 &');

and that did it. I've checked it back and forth multiple times and the second line works consistently while the first one fails every time.

PHP background process with && in command

The following will work

exec("(mongoexport ... | awk ... > my.csv && zip myzip my.csv) > /dev/null 2>&1 &")

It tells the shell to run all the sequence of commands enclosed in braces in the background process and redirect the output, so the exec function has nothing to wait for.

In your example, the shell launched from exec waited for the awk to finish and after that ran zip in background (if awk exited with status 0)



Related Topics



Leave a reply



Submit