Running a PHP "exec()" in the background on Windows?

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’s page on exec(), but wasn’t sure how to do that.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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


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')



Solution 2

$command = 'start /B program.exe  > NUL';
pclose( popen( $command, 'r' ) );

For more info:

Solution 3

Try the Windows ‘start‘ command:

Solution 4

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'";

execInBackgroundWindows('curl.exe','c:\temp','-X POST -H "Content-Type: application/json" -d @test.json');

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply