9

i try to run php processes in background and start this from an php file.

Some informations: PHP Version 5.2.17, php safe_mode is off, linux system. I start the process with exec, tried already shell_exec. I set all files to 0755, 0777.

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

If i print this statement, i get this and the pid is okay:

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

If i look for processes under ssh with

top

i see my php5 process with the correct pid. user is root

  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 start the process manual top 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 

The problem: it seems like nothing happens.To debug a little bit i wrote a output in the file

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

No logs in my file. If I start this file in my browser, it processes correctly and get my file.log with the "debugging" information.

Why this is working in browser and not on exec/shell_exec command??! i have exactly these php processes with correct pid, but no result.

Thanks a lot for help!

stoe
  • 93
  • 1
  • 1
  • 4
  • You should first try providing an absolute path to the script you're trying to execute, not a relative one like you seem to be doing now. Ie. use `nohup /usr/local/bin/php5 /absolute/path/to/file.php > /dev/null 2> /dev/null & echo $!` – wimvds Mar 13 '11 at 09:51
  • I have the absolute path to this file. I can start this nohup from every directory in ssh. its working. unfortunately not with php. – stoe Mar 13 '11 at 09:58
  • Then it could be a permission problem. So check your logs (Apache, php error log, console log)... If you're running AppArmor/SELinux try to turn it off to test if you need to change your security policy to allow these background tasks. – wimvds Mar 13 '11 at 10:26

3 Answers3

2

The PHP CLI environment can be different from the web environment (mod_php, FCGI, whatever). It's entirely possible for a script to die with an error when run from the commandline and not when you invoke it through a webserver. Debug your script. If you can, SSH into your server, su to the webserver user and run the script from the commandline yourself.

If you can't do that, set up your own development server where you can do this (it's not that hard if you know some Linux).

Sander Marechal
  • 22,978
  • 13
  • 65
  • 96
0

Except possible permission issue, a similar answer is posted here - https://stackoverflow.com/a/22705727/3471333.

nohup /usr/local/bin/php5 /.../../file.php </dev/null 2> /dev/null & echo $!
Community
  • 1
  • 1
chf
  • 51
  • 4
0
$arg1 = escapeshellarg("Hello World");
$pid = shell_exec("php file.php {$arg1} >/dev/null 2>&1 &");

file.php

$argument = $argv[1];
Optimaz Prime
  • 857
  • 10
  • 11