0

I need to run a command in PHP like this:

exec('dosomething > saveit.txt');

Except I don't want PHP to wait for it to be complete. I also don't want to throw away the output, and I don't want to use nohup because I'm using that for something else in the same directory.

I also tried pclose(popen('dosomething > saveit.txt','r')); and that didn't work, it still waited.

Alasdair
  • 13,348
  • 18
  • 82
  • 138
  • `nohup` can write to different output files. And if you immediately call `pclose` then the system of course must wait for the subprocess. – mario Nov 09 '11 at 09:39

2 Answers2

4

Add an ampersand to the end of the command, so:

exec('dosomething > saveit.txt &');
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
3

in the documentation of exec() there is an interesting comment that says:

Took quite some time to figure out the line I am going to post next. If you want to execute a command in the background without having the script waiting for the result, you can do the following:

 <?php
  passthru("/usr/bin/php /path/to/script.php ".$argv_parameter." >> /path/to/log_file.log 2>&1  &");
 ?>
naivists
  • 32,681
  • 5
  • 61
  • 85
  • Only problem is that I'm not trying to run another PHP script. But I can always make a PHP script with just the exec command in it, then run that PHP script with this. It's a solution. – Alasdair Nov 09 '11 at 09:38