0

I'm a webhosting owner, I don't know why currently, but I have some php scripts that are launched for many hours (as personnaly known customers), so I think there is a bug somewhere.

These scripts are eating the RAM AND the swap... So I'm looking for a way to list processes, find the execution time, kill them 1 by 1 if the execution exceed 10 or 20 minutes.

I'm not a bash master, but I know bash and pipes. The only thing I don't know, is how to list the processes (with execution time AND complete command line with arguments). Actually, even in top (then c) there is no arguments in php :/

Thanks for your help.

Max13
  • 919
  • 2
  • 9
  • 27
  • 1
    I'm a student in IT. I've founded a non-profit organization aiming at providing free/low-cost internet services. 1) I'm learning with such issues, 2) I'm the system admin currently. – Max13 Jun 14 '12 at 10:08
  • How are these php processes created? Are you using apache's mod_php, fastcgi, php-fpm? Maybe [ulimit](http://ss64.com/bash/ulimit.html) can help. – Doncho Gunchev Jun 15 '12 at 01:24

2 Answers2

4

If you are running Apache with mod_php, you will not see a separate PHP process since the script is actually running inside an Apache process. If you are running as FastCGI, you also might not see a distinguishable PHP process for the actual script execution, though I have no experience with PHP/FastCGI and might be wrong on this.

You can set the max_execution_time option, but it is overridable at run time by calling set_time_limit() unless you run in Safe Mode. Safe mode, however, has been deprecated in PHP 5.3 and removed in 5.4, so you cannot rely on it if you are on 5.4 or plan to upgrade.

If you can manage it with your existing customers (since in some cases it requires non-trivial changes to PHP code), running PHP as CGI should allow you to monitor the actual script execution, as each CGI request will spawn a separate PHP interpreter process and you should be able to distinguish between the scripts they are executing. Note, however, that CGI is the least effective setup (the others being mod_php and FastCGI).

lanzz
  • 42,060
  • 10
  • 89
  • 98
  • Thank your for your information. Okay... So, setting max_execution_time will block most of scripts. We're in CGI currently cause we had an issue with some scripts (like frapi, which uses APC, which does nothing in FCGI). We're running PHP as CGI with suExec, I know who is running a script but I can't know which one actually. If I knew how, maybe I will be able to trace a potential nucked script :/ . Thank you. – Max13 Jun 14 '12 at 11:46
  • BTW, max_execution_time is already at 30sec, Safe Mode is off. And scripts are still executing for hours sometimes... Currently, the server seems to be fine. – Max13 Jun 14 '12 at 11:49
  • How exactly do you determine that scripts are executing for hours? Since you're running as CGI, you should see a separate PHP process with a long running time — you should look at the `START` column in `ps aux` (which is the wall clock time when the process started) and not at `TIME` (which is CPU time and might be low if the process is mostly idle). – lanzz Jun 14 '12 at 11:54
1

You can use the ps -aux command to list the processes with some detailed information.

You can also check out the ps man page.

This might also be of some help.

Community
  • 1
  • 1
  • I thought about that, but here is the output: clanrvvi 28303 0.6 0.1 167736 12968 ? S 11:59 0:00 /usr/bin/php Nothing concerning the script loaded, but the time is there (0:00) – Max13 Jun 14 '12 at 09:59