How can I find out which PHP script a process is running in Linux?

How can I find out which PHP script a process is running in Linux?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Mapping PHP Scripts to Linux Processes in FastCGI Environments

Determining which specific PHP script a Linux process is executing is a common operational challenge, particularly when you are managing a production stack like Apache running PHP 5.3 via the FastCGI Handler (mod_fcgi or mod_fcgid). In this architecture, the distinction between the web server process and the application logic is blurred because the web server spawns a persistent PHP-FPM or CGI daemon that handles multiple requests sequentially. Unlike a simple script execution where the filename is obvious in the command line, FastCGI decouples the script path from the persistent process ID (PID) until the request actually arrives.

When you inspect a process using ps -ef or /proc/[pid]/cmdline, you will typically see the master PHP-FPM daemon or the Apache parent process. You will not immediately find /var/www/script.php in the command line arguments of the running child. This is by design; the FastCGI protocol allows the web server to pass the script name dynamically via the SCRIPT_FILENAME environment variable rather than relying on a static executable path.

To identify the specific script associated with a given PID, you must inspect the process's environment variables or standard input/output streams while it is actively handling a request. The most reliable method involves examining the /proc/[pid]/environ file. This file contains key-value pairs representing the environment passed to the child process. You should look for SCRIPT_FILENAME.

ps -p <PID> -o pid,args | grep php
cat /proc/<PID>/environ | tr '\0' '\n' | grep SCRIPT_FILENAME

If you are using Apache with mod_fcgi, the environment variable might also be exposed as FCGI_ROLE or passed through specific headers. However, simply looking at the PID in /proc often yields stale data because PHP-FPM pools handle multiple requests per worker. If your PHP version is 5.3, note that error logging behavior differs significantly from modern versions; you may need to enable display_errors=On temporarily or configure log_errors=On to capture stack traces that reveal the script path in the Apache error log (/var/log/apache2/error.log).

A critical pitfall in this environment is the concept of process pooling. A single PHP-FPM worker might execute /app/user/login.php for one request, then /app/admin/dashboard.php for the next. If you catch the process mid-execution and check its environment, you will get the script currently being processed. If the process is idle (waiting for a request), the SCRIPT_FILENAME variable may be empty or point to a dummy file defined in your configuration.

To avoid misidentifying processes, always cross-reference the PID with the specific request log (access.log). The access log entry corresponding to the time you are investigating will show the requested URI. You can then correlate that URI with the script path found in SCRIPT_FILENAME via /proc.

For developers working on legacy stacks like PHP 5.3, understanding this mapping is essential for debugging memory leaks or performance bottlenecks attributed to specific endpoints. If you require a more robust solution for high-volume traffic where manual log parsing becomes inefficient, consider upgrading your stack to a modern PHP version that supports better process isolation or utilizing tools like LaravelCompany for managed hosting environments that abstract these underlying complexities.

Our network