18

I have searched a lot to find the exact answer but didn't find any.

many people mentioned that we should & at end of command to don't wait for response.
for example to run bg.php in background , this was recommended:

exec("/usr/bin/php bg.php &");  

but it doesn't work for me. and the main script waits for complete execution of the bg.php.

I also read somewhere to write bg.php output in a logfile but my background script doesn't produce any output. It does some process and then write something in database.

I just want my script to run bg.php and don't wait for it to end.

please help me including correct code.

Aliweb
  • 1,891
  • 3
  • 21
  • 44

2 Answers2

23

You have to reroute programs output somewhere too, usually /dev/null

exec($cmd . " > /dev/null &");
ninaj
  • 748
  • 4
  • 9
  • works fine! but what is /dev/null directory? does this command write the output to a file? – Aliweb Oct 11 '12 at 15:26
  • 2
    /dev/null just discards the data sent to it, you can alternatively route it to some file if you need the program output – ninaj Oct 11 '12 at 15:32
1

Have you considered using screen? You can start up a screen session that runs in a detached process.

screen -d -m -S my_bg_session /usr/bin/php bg.php

Then you can connect to it later if it is still running:

screen -r my_bg_session
moodboom
  • 6,225
  • 2
  • 41
  • 45