3

I've created a script that uses psexec to call another script which calls psexec to run a command line program of mine.

The reason for so many calls to psexec and other scripts is solely so that my PHP script doesn't have to wait for the process to finish before finishing it's output to the browser.

Is there a way I can do this without needing to use psexec? I'm having issues with psexec so I'd like to just completely remove it from my program.

I'm running Windows 2008

EDIT: I changed the title, I guess this would be a more accurate title. I found out the If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends. on php.net's page on exec(), but wasn't sure how to do that.

Rob
  • 7,980
  • 30
  • 75
  • 115
  • What exactly are you trying to do? Just curious because I'm not sure there's a way to pull it off other than use background scripts... – espradley Oct 07 '11 at 20:04

4 Answers4

6

Include a redirection in the command line:

exec('program.exe > NUL')

or you could modify your program to explicitly close standard output, in C this would be

CloseHandle(GetStdHandle(STD_OUTPUT_HANDLE));

It is possible (the documentation doesn't say) that you might need to redirect/close both the standard output and standard error:

exec('program.exe > NUL 2> NUL')

or

CloseHandle(GetStdHandle(STD_OUTPUT_HANDLE));
CloseHandle(GetStdHandle(STD_ERROR_HANDLE));
Harry Johnston
  • 35,639
  • 6
  • 68
  • 158
4
$command = 'start /B program.exe  > NUL';
pclose( popen( $command, 'r' ) );

For more info: http://humblecontributions.blogspot.com/2012/12/how-to-run-php-process-in-background.html

Niko
  • 813
  • 8
  • 11
2

Try the Windows 'start' command: http://technet.microsoft.com/en-us/library/cc770297%28WS.10%29.aspx

Boann
  • 48,794
  • 16
  • 117
  • 146
  • If I try using `start` from `exec()`, won't I still need to wait for that process to finish before the PHP script will finish? – Rob Oct 07 '11 at 20:16
  • 2
    @Rob Oh sorry, I tried it and you're absolutely right. That's odd. I really thought it started independently. – Boann Oct 07 '11 at 20:21
  • 6
    If you use popen( 'start command', 'r' ) on windows then it WON'T wait for the command to finish. Just found this out a few days ago. – Jerry Saravia Mar 22 '12 at 19:55
  • like http://php.net/manual/en/function.popen.php here? the `Example #2 popen() example` , this is what you say?? – Damian Sep 30 '15 at 17:43
1

Using start /B has certain limitations when calling scripts that are spawning sub-processes. The following method spawns a sub-process using PowerShell to handle such cases:

function execInBackgroundWindows($filePath, $workingDirectory, $arguments) 
{
    $cmd = "powershell.exe Start-Process -FilePath $filePath -WorkingDirectory $workingDirectory -ArgumentList '$arguments'";
    shell_exec($cmd);
} 

execInBackgroundWindows('curl.exe','c:\temp','-X POST -H "Content-Type: application/json" -d @test.json http://127.0.0.1:4000/myapp');
atyachin
  • 1,103
  • 1
  • 12
  • 14