Nohup: run a php process in the background

I am trying to run php processes in the background and run this from a php file.

Some information: PHP Version 5.2.17, php safe_mode off, Linux system. I start the process using exec, already tried shell_exec. I set all the files to 0755, 0777.

$pid = exec("nohup $cmd > /dev/null 2> /dev/null & echo $!"); 

If I print this expression, I get this, and pid is fine:

 nohup /usr/local/bin/php5 /.../../file.php > /dev/null 2> /dev/null & echo $! 

If I am looking for processes in ssh with

 top 

I see my php5 process with the correct pid. root user

  PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 3533 xxxxxxxx 20 0 21356 8868 4580 S 0 0.4 0:00.13 php5 3536 xxxxxxxx 20 0 20836 8260 4428 S 0 0.4 0:00.09 php5 3539 xxxxxxxx 20 0 20836 8260 4428 S 0 0.4 0:00.09 php5 3542 xxxxxxxx 20 0 20836 8260 4428 S 0 0.4 0:00.09 php5 3545 xxxxxxxx 20 0 20836 8260 4428 S 0 0.4 0:00.09 php5 3548 xxxxxxxx 20 0 20836 8260 4428 S 0 0.4 0:00.09 php5 3551 xxxxxxxx 20 0 20836 8260 4428 S 0 0.4 0:00.09 php5 

if I run the top of the process manual, it looks like this:

 PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 8141 xxxxxxxx 22 2 24048 9.9m 5344 S 10 0.5 0:00.31 php5 

Problem: Nothing seems to be happening. To debug a bit, I wrote the output in a file

 ob_start(); echo "STARTING..."; writeLog(ob_get_contents()); //... function writeLog($info){ $handle = fopen("file.log", "a+"); fwrite($handle, $info); fclose($handle); } exit; 

There are no logs in my file. If I run this file in my browser, it correctly processes and receives the file.log file with the "debug" information.

Why does this work in the browser and not in the exec / shell_exec command ?! I have exactly these php processes with the correct pid, but there is no result.

Thank you for help!

+6
php process cron background nohup
source share
2 answers

The PHP CLI may be different from the web (mod_php, FCGI, whatever). It is possible that the script will die with an error when launched from the command line, and not when it is called through the web server. Debug the script. If you can SSH to your server, contact the web server user and run the script from the command line yourself.

If you canโ€™t do this, set up your own development server where you can do it (itโ€™s not so difficult if you know some kind of Linux).

+1
source share

Except for a possible resolution problem, a similar answer is posted here - fooobar.com/questions/595938 / ....

 nohup /usr/local/bin/php5 /.../../file.php </dev/null 2> /dev/null & echo $! 
0
source share

All Articles