1

On linux apache server ( ubuntu 14.04 lts, apache 2.4.7 with mpm_prefork and mod_php) I have PHP scripts that takes a long time. These are killed by apache.

We have tune php setting (max_execution_time, set_time_limit...)

We haven't any trace in log (syslog, apache access/error log)

We have traced apache process with strace :

2172 is the script process
1939 is the apache main process
....

2172  14:53:01 +++ killed by SIGKILL +++
1939  14:53:01 --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_KILLED, si_pid=2172, si_status=SIGKILL, si_utime=3067, si_stime=38} ---
m4rtin
  • 2,445
  • 22
  • 34
Inexine
  • 11
  • 2
  • I suggest long running php scripts should be run in the background (not in the web-server) and should 'leave clues' in a database as to the 'state' of progress. This can be reported in the web-browser in a 'user-friendly' way. – Ryan Vincent Oct 01 '14 at 15:45

2 Answers2

1

try set ini_set('max_execution_time', -1); or run this script as root, then apache won't kill him

Daredzik
  • 422
  • 2
  • 9
  • 21
  • We have try max_execution_time -1 but same result. And run script as root on bash... the script was not killed. – Inexine Oct 01 '14 at 15:29
  • 20-30 minutes it depend on processing Apache kill the script randomly. 2 min, 10 min , 15 min... An in few case script isn't killed – Inexine Oct 01 '14 at 16:01
  • try: `header('Content-Type: text/html; charset=utf-8'); ini_set('max_execution_time', -1); ini_set('memory_limit', '512M');` – Daredzik Oct 01 '14 at 16:25
  • KEEP ALIVE is on ? in configuration ? – Daredzik Oct 01 '14 at 17:02
  • yes : KeepAlive On MaxKeepAliveRequests 500 (test with 100) KeepAliveTimeout 3 (test with 5) – Inexine Oct 01 '14 at 19:33
0

Another possibility is Apache2 is killing the process because it's not sent anything back for a certain amount of time. This often happens on shared hosting. If you're using output buffering, turn that off. Then every so often print out something and immediately use flush() to send the information back to Apache.

For example; in your longest loop you can do the following:

$time = time();
while($looping) {
  ... Code here ...
  if(time() > $time) {
    echo '.';
    flush();
    //ob_flush();//If you're using output buffering (often on by default)
    $time = time();
  }
}
Nathan
  • 331
  • 2
  • 4
  • we try with this in while : echo "
    "; echo memory_get_usage(); flush(); ob_flush(); $cpt=0;
    – Inexine Oct 01 '14 at 15:42
  • That should work. Also try using `set_time_limit(30)`, which will increase the time the script has to run by 30s – Nathan Oct 01 '14 at 16:13