exec() waiting for a response in PHP [duplicate]
Possible Duplicate:
php exec command (or similar) to not wait for result
I have a page that runs a series of exec()
commands which for开发者_StackOverflow中文版ces my PHP script to halt alteration until it receives a response. How can I tell exec()
to not wait for a response and just run the command?
I'm using a complex command that has a backend system I can query to check the status, so I'm not concerned with a response.
Depends on what platform you are using, and the command you are running.
For example, on Unix/Linux you can append > /dev/null &
to the end of the command to tell the shell to release the process you have started and exec will return immediately. This doesn't work on Windows, but there is an alternative approach using the COM object (See edit below).
Many commands have a command line argument that can be passed so they release their association with the terminal and return immediately. Also, some commands will appear to hang because they have asked a question and are waiting for user input to tell them to continue (e.g. when running gzip
and 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 the question (in the gzip
example you would pass -f
).
EDIT
Here is the code to do what you want on Windows, as long as COM
is available:
$commandToExec = 'somecommand.exe';
$wshShell = new COM("WScript.Shell");
$wshShell->Run($commandToExec, 0, FALSE);
Note that it is the third, FALSE
parameter that tells WshShell to launch the program then return immediately (the second 0
parameter is defined as 'window style' and is probably meaningless here - you could pass any integer value). The WshShell object is documented here. This definitely works, I have used it before...
I have also edited above to reflect the fact that piping to /dev/null
is also required in order to get &
to work with exec()
on *nix.
Also just added a bit more info about WshShell.
In the past, I've had good luck with constructs like the following (windows, but I'm sure there's an equivalent command in *nix
pclose(popen('START /B some_command','r'));
What about running command in background ?
exec('./run &');
精彩评论