Friday, 5 December 2008

PHP: How to execute a command in the background on Windows?

So the problem is: how to fire and forget a command/batch etc in PHP asynchronously (or in the background)? That is, the calling PHP script should not wait for the command to finish. Rather the PHP script would launch the command, then terminate. Meanwhile the command is running in the background.

There are many examples and techniques on the Internet on how to execute a command in *NIX. It could be as simple as appending an ampersand (&) to the command.

However, since I am running Windows, I needed one that works for Windows... duh :) right?
After much testing, trying, looking around, the only way I found to achieve this is the following:

pclose(popen("start cmd", "r"));

Some variations of this approach are presented here...

All the variations of PHP function exec() will result in synchronous execution (i.e. the PHP script hangs until the command terminates).

No comments:

Post a Comment