5

Possible Duplicate:
php exec command (or similar) to not wait for result

I have a page that runs a series of exec() commands which forces 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.

Community
  • 1
  • 1
Ben
  • 60,438
  • 111
  • 314
  • 488

3 Answers3

7

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.

DaveRandom
  • 87,921
  • 11
  • 154
  • 174
1

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'));
Dereleased
  • 9,939
  • 3
  • 35
  • 51
  • I have had problems with this approach with certain commands - notably launching another PHP CLI process. Using the COM approach above seems to work across the board. – DaveRandom Aug 17 '11 at 13:45
  • When launching another PHP process I typically do something like `pclose(popen('START /B CMD /C START C:\php5\php.exe -c c:\Apache\Apache2.2\bin -f ', 'r'));` – Dereleased Aug 17 '11 at 14:39
0

What about running command in background ?

exec('./run &');
hsz
  • 148,279
  • 62
  • 259
  • 315
  • `exec` has nothing to do with shell meta-characters like `&`. This will simply sun `./run` with the arguments `['./run', '&']`. – phihag Aug 17 '11 at 13:24
  • @phihag true, but you can use shell_exec or backticks to do work around that, can't you? – DaveRandom Aug 17 '11 at 13:28
  • @DaveRandom Nope, `shell_exec` waits for the child to exit too. – phihag Aug 17 '11 at 13:30
  • @phihag that's not right is it? I'm fairly sure that would execute run and run it in the background if exec was executed on a linux box? – Alan Hollis Aug 17 '11 at 13:31
  • @user346271 Just try it yourself: `time php -r 'shell_exec("sleep 2 &");'` – phihag Aug 17 '11 at 13:34
  • 1
    @phihag you're right, however piping the output to /dev/null works as intended. `exec('sleep 20 > /dev/null &');` returns straight away. – Alan Hollis Aug 17 '11 at 13:41
  • @phihag You are absolutely right, although as previously mentioned if you redirect STDOUT to `/dev/null` and then include `&` it does work as desired. – DaveRandom Aug 17 '11 at 13:47
  • I don't think it's quite that simple to get things to actually get things to run in the background from within PHP. [The PHP manual on exec()](http://www.php.net/manual/en/function.exec.php) notes `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.` [Here](http://www.php.net/manual/en/function.exec.php#101506) is an example of the extra arguments that enable background-running. – Jeffrey Blake Aug 17 '11 at 13:54