2

I'm using the exec function in PHP to run a command. The command I'm running can often take quite a bit of time and I don't have any need to read it's output. Is there a simple way of telling PHP not to wait for the exec command to finish before moving on with the rest of the script?

5
  • do it as a separate process perhaps? Commented Sep 28, 2012 at 7:19
  • How would I go about doing that? Do I just append & to the end of the command? Commented Sep 28, 2012 at 7:25
  • 1
    exec("nohup $your_command &") - run command immune to hangups, output to non-tty (nohup), run it in background (&) Commented Sep 28, 2012 at 7:29
  • @Nemoden: Awesome! Thanks. So each time I call that it will start a new process, correct? Is there a similar way to run these on separate threads? Commented Sep 28, 2012 at 7:31
  • yes, new process will start, I'll demonstrate it in an answer. Commented Sep 28, 2012 at 7:42

3 Answers 3

2
// nohup_test.php:

// a very long running process
$command = 'tail -f /dev/null';
exec("nohup $command >/dev/null 2>/dev/null &"); // here we go
printf('run command: %s'.PHP_EOL, $command);
echo 'Continuing to execute the rest of this script instructions'.PHP_EOL;

for ($x=1000000;$x-->0;) {
  for ($y=1000000;$y-->0;) {
    //this is so long, so I can do ps auwx | grep php while it's running and see whether $command run in separate process
  }
}

run nohup_test.php:

$ php nohup_test.php
run command: tail -f /dev/null
Continuing to execute the rest of this script instructions

Let's find out pids of our processes:

$ ps auwx | grep tail
nemoden   3397  0.0  0.0   3252   636 pts/8    S+   18:41   0:00 tail -f /dev/null
$ ps auwx | grep php
nemoden   3394 82.0  0.2  31208  6804 pts/8    R+   18:41   0:04 php nohup_test.php

as you can see, pid is different and my script is running without waiting for tail -f /dev/null.

Sign up to request clarification or add additional context in comments.

2 Comments

Using /dev/null was key to getting this working correctly. For some reason when I specified an actual log file location (i.e. /path/to/logfile as suggested by @zaf), PHP still waited for the process to finish. Any ideas why this is?
if you specify a log file like so: exec("nohup $command >/tmp/my.log 2>/dev/null &"); and you run php nohup_test.php it doesn't print Continuing to execute the rest of this script instructions ? Because it works just fine or... should work so... It's really odd otherwise
1

Here is what I use (you can use exec or system instead of paasthru):

passthru("/path/to/program args >> /path/to/logfile 2>&1 &");

2 Comments

Thanks! I'm not too familiar with some of the syntax here, though. What does the >> and 2>&1 & do?
>> will send the output of the program to the log file. The '2>&1' will redirect error messages to the standard output, basically meaning all output will go to the log file. The '&' means run the command in the background.
1

What you are looking for is called an Asynchronous call, like answered here:

Asynchronous shell exec in PHP

php execute a background process

PHP exec() as Background Process (Windows Wampserver Environment)

1 Comment

Thanks! I searched quite a bit for this before posting but apparently never used the right keywords...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.