1

I have recently created a script that needs to be run on background.. I use this code to run it...

function run_in_background($Command, $Priority = 0) {
   if($Priority)
       $PID = exec("nohup nice -n $Priority $Command 2> /dev/null & echo $!");
   else
       $PID = exec("nohup $Command 2> /dev/null & echo $!");
   return($PID);
}

and use it like this

run_in_background('curl http://www.mydomain.com/mypage.php',5);

Unfortunately, it is not running on background...

I have also used:

shell_exec

instead of

exec

But it still doesn't work.. Is there something wrong with my setup?..

Note: it doesn't run in background but it sure runs when the page is loaded...

thanks in advanced..

Jeffrey Monte
  • 680
  • 1
  • 5
  • 12
  • 1
    please be very careful with this function, and clearly document how to use it properly. if users can control the value of $Command or $Priority, then you have a command injection vulnerabiliity. attackers will be able to execute commands on your web server os with the same permissions under which php runs. – atk Jan 11 '11 at 05:39

2 Answers2

0

Place a & at the end of your command to tell the shell to run the process in the background.

$PID = exec("nohup nice -n $Priority $Command 2> /dev/null & echo $! &");

I also asked and answered a similar question you could look over that may help you: How can I run a PHP script in the background after a form is submitted?

Community
  • 1
  • 1
Michael Irigoyen
  • 22,513
  • 17
  • 89
  • 131
0

Please see this answer, seems to be exactly what you are looking for:

php execute a background process

Community
  • 1
  • 1
Or Weinberger
  • 7,332
  • 23
  • 71
  • 116