Hasan Veli Soyalan Hasan Veli Soyalan - 1 year ago 117
Linux Question

Tracking php process on framework

I have a php framework is running on linux machine basicly every requests redirect to index.php by .htaccess

Options +FollowSymLinks

IndexIgnore */*
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*jpg$|!.*gif$|!.*png$|!.*jpeg$|!.*bmp$

RewriteRule . index.php

One of my php started to run %100 CPU i want to track which progress is that but when i check process with

ps aux | grep 23791

user 23791 0.3 0.8 30460 15288 ? S 12:32 0:01 /usr/bin/php /home/user/public_html/index.php

As normal, request redirect to index.php.But i have to find which request is this.

Is there any way to debug this problem ?


Answer Source

I just find my own question while im searcing for this issue. This time i have an answer.

If you want to see details of your progress on linux console via ps you have to use proctitle this pecl extension will give you

setproctitle() function.

extension is very old but still working even zts mode.

If you are using cli version you can use : cli-set-process-title() this is native php function (version PHP5 >=)

So simple code that i use to track on linux :

if ((PHP_SAPI === 'cli' OR defined('STDIN'))) {
    $tmp = $_SERVER["argv"];
    $text =  "/". implode('/',$tmp);
    @cli_set_process_title (urldecode($text));
} else {
    @setproctitle (urldecode($_SERVER["REQUEST_URI"]));

I just write this to top index.php on codeigniter.

For see processes uses most cpu and memory at linux :

ps o pid,stat,time,pcpu,pmem,cmd -C php --sort -pcpu

now you will able to see processes

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download