1

Possible Duplicate:
php in background exec() function

I am trying to run two exec commands from PHP. One is a script that might run for a few minutes and there can only be once instance of it. The second exec just checks if the first process is running and either lets you run it again or redirects you. The code is something like this.

This is the main process

$command = '/home/user/active/Nem-Swd.elf 30 > /dev/null 2>&1 & echo $!';

This is the process that checks if the other one is running already.

exec("ps ax | grep $name 2>&1", $output);

I know that in the php API it says :

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.

But I don't understand what that implies.

Community
  • 1
  • 1
vman
  • 321
  • 4
  • 10
  • What that means is that you need to redirect output to file (text file, xml file, ect) in order for your exec statement to continue running after the PHP program exits. – Kyle Aug 10 '11 at 14:55

1 Answers1

1

It means that if you run the exec command without re-routing its output, your current PHP script will wait for that command to finish before terminating the script. Since you are routing the output to standard out, I believe you should be ok. If you just ran exec($commandJob) you then would run into this problem.


Seems that this question has already been answered:
php in background exec() function

At a minimum (for example) you need to do this to make it run in the background:
exec("php test.php &");


There are other ways to start processes that might be worth looking into as well: http://www.php.net/manual/en/book.pcntl.php
http://php.net/manual/en/function.popen.php

Community
  • 1
  • 1
afuzzyllama
  • 6,538
  • 5
  • 47
  • 64
  • The why is it still waiting until the first process completes? – vman Aug 10 '11 at 15:11
  • so you run two execs in a row, but the second one never runs? – afuzzyllama Aug 10 '11 at 15:14
  • The second one runs only when the first one is complete. – vman Aug 10 '11 at 15:22
  • What happens if you do something like `exec($commandJob.' > command.out')`? – afuzzyllama Aug 10 '11 at 15:42
  • same thing it waits until the first one is finished. – vman Aug 10 '11 at 15:48
  • I have added the full command now, there is an ampersand before the echo. If I run this in the terminal it works exactly like it's supposed to do so. When php is executing it, it is not running in the background. – vman Aug 10 '11 at 18:08
  • Try running is like this? `exec('/home/user/active/Nem-Swd.elf 30 &')` – afuzzyllama Aug 10 '11 at 20:40
  • Yea i tried that with no success php still waits until it is over before running the second process. On top of it that shouldnt work technically since you have to redirect the output. which I tried with exec('/home/user/active/Nem-Swd.elf 30 & 2>&1); with no success. – vman Aug 10 '11 at 20:47
  • Unfortunatly I can't do that. I am running php and apache on a circuit board so it will be a pain to cross compile php with pcntl enabled. – vman Aug 10 '11 at 21:08
  • 1
    maybe you need to use something like one of these: http://www.php.net/manual/en/book.pcntl.php or http://php.net/manual/en/function.popen.php – afuzzyllama Aug 10 '11 at 21:10
  • It worked with popen thanks a lot. So basically I used `popen('/home/user/active/Nem-Swd.elf &','r');` – vman Aug 10 '11 at 21:32