0

I am trying to create a tarball from a php5 script under Linux, and I don't really care for the output; the way I have so far found immediately is to simply

system("tar czf tarball.tgz directory/path &");

However I would like to background the process

Checking system() documentation it mentions having to redirect the output to file

However

system("tar czf tarball.tgz directory/path > /dev/null 2>&1");

doesn't help. The system() function does not take a file descriptor... what am I missing?

Testing with these:

script test.php

<pre><?php

exec("bash dodate 2>&1 /dev/null &");
system("echo done at \$(date)");

?></pre>

Script ./dodate

sleep 5
date

I go to my browser and call/refresh the page; it takes indeed 5 seconds thenprints/updates the "done" message.

Thanks

taifwa
  • 302
  • 2
  • 10
  • That `&` at the end of your first snippet should make it run in the background - does it not? Do you need to report the output/result to your PHP program, or can it just continue without knowing? – mopo922 Jan 13 '16 at 23:15
  • If you don't care about the output, `exec()` may be a better option for you: http://php.net/manual/en/function.exec.php – mopo922 Jan 13 '16 at 23:16
  • Alas, the ampersand does not background the process... specifically I tested this by creating a script "dodate" with contents `sleep 5 ; date` and then calling `system("bash dodate &")` but it waits and then prints the output...! Same effect with exec – taifwa Jan 13 '16 at 23:18

1 Answers1

0

You "don't have" threads in php. One trick you can do is to do a curl request to another php that does what you want. You'll need to make sure that your curl times out pretty soon, and that the other php doesn't die when the http connection to it is closed by the curl timeout.

You can also read about the topic here: cURL Multi Threading with PHP or cURL Multi Threading?

Community
  • 1
  • 1
Gavriel
  • 18,880
  • 12
  • 68
  • 105