Checking if process still running?

Checking if process still running?

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

Stefan Bogdanescu

Founder & Senior Architect

Checking if a Process Is Still Running: Why ps and grep Behave Differently

Building a poor-man's watchdog script is a common requirement for maintaining uptime on legacy systems or constrained environments where dedicated process managers like systemd or Supervisor are unavailable. You need a CLI script that runs periodically via cron to verify an application is alive, restarting it immediately if it has crashed. While the goal seems straightforward, implementing reliable process detection often trips up developers due to subtle shell quirks and regex behaviors.

A frequent starting point involves checking the process list directly. Consider a scenario where you attempt to detect a specific executable or argument pattern using ps. A naive approach might look like this:

exec("ps -C $file -o pid=", $pids);
if (count($pids) > 1) {
    $exists = true;
}

The logic here assumes that if a file name matches an entry in the process list, the process is running. However, this method is fragile. The -C flag in ps matches the full command line of the process. If you search for a generic executable like /usr/bin/php, it will match not only your PHP script but also every other PHP instance on the server, including the one executing the check itself. This often leads to false positives where the script thinks a target application is running when it isn't, simply because the CLI interpreter matching the argument exists in the list.

Another common pattern involves piping grep to filter the output of ps. For example:

exec("ps -A | grep -i 'lighttpd -D' | grep -v grep", $output);

While this looks robust, it can fail silently depending on how the output is handled. In PHP 5.2 (the environment referenced in typical legacy scenarios), exec() returns the last line of output from the command if no variable is provided, but when capturing into an array, you must be precise about what constitutes a "hit."

The critical pitfall arises from how grep behaves when no matches are found. If the target process does not exist, ps -A | grep 'pattern' returns nothing. However, if your script relies on checking the length of the array returned by exec(), you must ensure that an empty result is treated distinctly from a match. In many implementations, if grep finds no matches, it exits with a non-zero status, but the shell pipeline might still pass an empty string or nothing to the variable depending on how the command substitution is wrapped.

The solution demonstrated in working examples often shifts the strategy slightly:

exec("ps aux | grep -i 'lighttpd -D' | grep -v grep", $pids);
if(empty($pids)) {
    print "Lighttpd not running!\n";
} else {
    print "Lighttpd OK\n";
}

This works because it explicitly checks if the resulting array is empty. If grep finds no matches, $pids remains empty (or contains only a single false entry depending on implementation), allowing the script to trigger the restart logic correctly. The grep -v grep part is essential here; without it, the command would match its own invocation in the process list, creating a loop where the check itself looks like evidence of the running process.

When writing these scripts for production, accuracy matters more than brevity. Always account for the fact that process names might contain spaces or special characters, which requires quoting arguments properly in both the PHP string and the shell command. Furthermore, relying solely on process name matching can be ambiguous if multiple programs share binary names. For robustness, checking the process ID (PID) against a known file descriptor or verifying the full command line path is safer than relying purely on substring matching.

If you are managing a fleet of servers with Laravel applications and need to ensure stability without external agents, integrating this logic into your deployment scripts is a viable strategy. For more complex orchestration needs, platforms like https://laravelcompany.com often provide insights into modernizing legacy monitoring stacks, but for immediate CLI needs, mastering ps and grep nuances remains a fundamental skill for system reliability.

Our network