I have a large PHP application and I'm looking for a way to know which PHP script is running at a given moment. Something like when you run "top" on a Linux command line but for PHP.
3 Answers
Are you trying to do so from within the PHP application, or outside of it? If you're inside the PHP code, entering debug_print_backtrace();
at that point will show you the 'tree' of PHP files that were included to get you at that point.
If you're outside the PHP script, you can only see the one process that called the original PHP script (index.php or whatnot), unless the application spawns parallel threads as part of its execution.

- 6,715
- 5
- 44
- 68
-
1@Byron: Yes it does: http://us3.php.net/manual/en/function.pcntl-fork.php (needs to be compiled with PCNTL, but it's a possibility) – MidnightLightning Jan 13 '11 at 21:35
-
nah that is forking the process, similar but not the same as threading. You can read some user feedback on this issue here: http://stackoverflow.com/q/209774/398519 – Jim Jan 13 '11 at 21:38
-
Okay, so I just used the wrong term; PHP can "split off another process", which the point is would show up in `top` or similar as a separate process, which was what the OP may have been wanting, though I'm guessing it's the first half of my answer they need. – MidnightLightning Jan 13 '11 at 21:45
If you're looking for this information at the system level, e.g. all php files running under any Apache child process, or even any PHP files in use by other apps, there is the lsof
program (list open files), which will spit out by default ALL open files on the system (executables, sockets, fifos, .so's, etc...). You can grep the output for '.php' and get a pretty complete picture of what's in use at that moment.

- 356,200
- 43
- 426
- 500
This old post shows a way you can wrap your calls to php scripts and get a PID for each process.
$cmd = 'nohup nice -n 10 /usr/bin/php -c /path/to/php.ini -f /path/to/php/file.php action=generate var1_id=23 var2_id=35 gen_id=535 > /path/to/log/file.log & echo $!';
$pid = shell_exec($cmd);