# How can I view all PHP-processes

## mariourk

I've been playing around with a small php-script that runs in an endless-loop:

```

<?php

while( true )

{

    send_an_email();

    sleep( 10 );

}

?>

```

I did this to get a better understanding of abuse (f.e. a hacked server)

I called this script with my browser and to my surprise it keeps running forever,

even when I close the browser. I had to restart Apache to stop it.

Now I wonder, is it possible to see all the php-scripts that are currently running

on a server and kill that specific script?

----------

## durian

 *mariourk wrote:*   

> 
> 
> Now I wonder, is it possible to see all the php-scripts that are currently running
> 
> on a server and kill that specific script?

 Won't they just show up in the process list on the server (eg. "ps -ef | grep php" or something)?

-peter

----------

## malern

Have you altered your php.ini? Normally your script should not continue to run forever. Unless you have changed max_execution_time to 0, and set ignore_user_abort to "On".

Also how have you configured Apache to run PHP, as a module or via CGI?

----------

## mariourk

 *Quote:*   

> 
> 
> Won't they just show up in the process list on the server (eg. "ps -ef | grep php" or something)? 
> 
> 

 

Unfortunately, no. as far as I know it is not possible to see every inividual running php-script (or process)

I would be very happy if someone could tell me if this is possible and how it works.

 *Quote:*   

> 
> 
> Have you altered your php.ini? Normally your script should not continue to run forever.
> 
> Unless you have changed max_execution_time to 0, and set ignore_user_abort to "On".
> ...

 

The settings are:

```

max_execution_time = 30     ; Maximum execution time of each script, in seconds

; ignore_user_abort = On

```

That last option is turned off (commented out)

 *Quote:*   

> 
> 
> Also how have you configured Apache to run PHP, as a module or via CGI?
> 
> 

 

As far as I know, apache is configured to run PHP as a module.

----------

## Mad Merlin

 *mariourk wrote:*   

>  *Quote:*   
> 
> Won't they just show up in the process list on the server (eg. "ps -ef | grep php" or something)? 
> 
>  
> ...

 

Well, with mod_php, PHP is running in the context of Apache, so Apache processes that are running may or may not be executing PHP. Apache can give you more information via mod_status: http://httpd.apache.org/docs/2.2/mod/mod_status.html

----------

## malern

 *mariourk wrote:*   

> 
> 
> The settings are:
> 
> ```
> ...

 

Ah, I've just read the docs, and apparently the time spent running the sleep() command isn't counted towards the max_execution_time. So your script should eventually terminate, but it'll take a lot longer than 30 seconds.

Also, I'm still not sure why your script doesn't terminate when you close your browser. I've tried your script on my machine and it exits a few seconds after I close the browser window. What browser are you using?

As for listing running php scripts, you may be able to use lsof to list all the apache processes that have a certain script file open. Assuming they keep it open for the entire time they are running it. But if you are using any sort of PHP caching (like APC), this might not work.

----------

## mariourk

 *Quote:*   

> 
> 
> Apache can give you more information via mod_status:
> 
> 

 

This seems to go in the right direction. Mod_status does show my php-script, that is running indefinitely.

Including the name of the file. Unfortunately, the pid is the pid of apache. So, killing the specific script

seems not possible. But at least now I can see what script is causing trouble.  :Very Happy: 

----------

## Mad Merlin

 *mariourk wrote:*   

>  *Quote:*   
> 
> Apache can give you more information via mod_status:
> 
>  
> ...

 

If you're using the prefork MPM (the default), then Apache spawns multiple processes, one for each concurrent request it's serving, so you can actually just kill the offending process.

----------

