Exec () is waiting for a response in PHP

Possible duplicate:
php exec command (or similar) so as not to wait for the result

I have a page that runs a series of exec() commands that cause my PHP script to pause the change until a response is received. How can I tell exec() not to wait for an answer and just run the command?

I use a complex command that has a backend system that I can request to check the status, so I'm not interested in the answer.

+4
source share
3 answers

Depends on the platform you are using and the team you are using.

For example, on Unix / Linux, you can add > /dev/null & at the end of the command to tell the shell to free the process you started and exec will return immediately. This does not work on Windows, but there is an alternative approach using a COM object (see below).

Many commands have a command line argument that can be passed, so they release their connection to the terminal and return immediately. In addition, some commands will freeze because they have asked a question and are waiting for user input to let them continue (for example, when gzip started, the target file already exists). In these cases, there is usually a command line argument that can be passed to tell the program how to handle this and not ask a question (in the gzip example, you will pass -f ).

EDIT

Here is the code to be done on Windows if COM is available:

 $commandToExec = 'somecommand.exe'; $wshShell = new COM("WScript.Shell"); $wshShell->Run($commandToExec, 0, FALSE); 

Note that this is the third parameter FALSE , which tells WshShell to start the program, and then returns immediately (the second parameter 0 is defined as the "window style" and is probably pointless here - you can pass any integer value). The WshShell object is documented here . It definitely works, I used it before ...

I also edited above to reflect the fact that & for> for> for /dev/null also requires a switch to /dev/null .

Also added a bit more information about WshShell.

+6
source

In the past, I was lucky with constructs like the following (windows, but I'm sure there is an equivalent command in * nix

 pclose(popen('START /B some_command','r')); 
+1
source

How about running a command in the background?

 exec('./run &'); 
0
source

All Articles