Process Monitoring
System Administration
Task Management
Command Line Tools
Operating Systems

How can I know if a process is running?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

Checking whether a process is running sounds simple, but the best method depends on what you know already. Sometimes you know the process name, sometimes you know the PID, and sometimes you need a scriptable check rather than a one-off terminal command.

Check by Name on Unix-Like Systems

If you know the executable name, pgrep is usually the cleanest tool on Linux and macOS.

bash
pgrep -a nginx

If the process exists, pgrep prints one or more matching PIDs and often the command line. If no match exists, it exits with a nonzero status.

That makes it useful in shell scripts:

bash
1if pgrep -x nginx >/dev/null; then
2  echo "nginx is running"
3else
4  echo "nginx is not running"
5fi

The -x flag asks for an exact name match, which helps avoid partial matches such as nginx-helper.

Check by PID

If you already know the process ID, use kill -0 on Unix-like systems. It does not terminate the process. It only asks the kernel whether the PID exists and is accessible.

bash
1PID=12345
2
3if kill -0 "$PID" 2>/dev/null; then
4  echo "process exists"
5else
6  echo "process does not exist"
7fi

This is especially useful when a service writes a PID file and you want to confirm whether that PID is still alive.

Inspect With ps

ps is more verbose than pgrep, but it gives you more detail:

bash
ps -ef | grep '[n]ginx'

The bracket trick prevents grep from matching itself. This approach is useful when you want additional context such as:

  • parent PID
  • start time
  • command line arguments

For a simple yes-or-no check, pgrep is usually cleaner. For investigation, ps is often better.

Windows Equivalents

On Windows, use tasklist or PowerShell.

Command Prompt:

bat
tasklist | findstr /I notepad.exe

PowerShell:

powershell
Get-Process -Name notepad -ErrorAction SilentlyContinue

For a boolean-style PowerShell check:

powershell
1if (Get-Process -Name notepad -ErrorAction SilentlyContinue) {
2    "running"
3} else {
4    "not running"
5}

PowerShell is usually easier to script because it returns objects instead of plain text.

Check From Python

If you need the answer inside a program, psutil is a practical cross-platform option.

python
1import psutil
2
3
4def is_process_running(name: str) -> bool:
5    for proc in psutil.process_iter(["name"]):
6        if proc.info["name"] == name:
7            return True
8    return False
9
10
11print(is_process_running("python"))

If you know the PID instead:

python
1import psutil
2
3pid = 12345
4print(psutil.pid_exists(pid))

That is often cleaner than spawning shell commands from application code.

Prefer Service-Aware Checks When Appropriate

If the process is managed by a supervisor, ask the supervisor instead of the raw process table. For example, on systemd systems:

bash
systemctl is-active nginx

That tells you whether the service is active according to the service manager, which is often more meaningful than checking for a stray PID manually.

The same principle applies to containers and orchestrated workloads. If the workload is managed by Docker or Kubernetes, use those tools when what you really care about is service state rather than a single host process.

Match the Right Process

Process names are not always unique. A machine may have several python, java, or node processes at once. In that case, check the command line too.

bash
pgrep -af "python.*worker.py"

Or in Python:

python
1import psutil
2
3for proc in psutil.process_iter(["pid", "name", "cmdline"]):
4    cmdline = " ".join(proc.info.get("cmdline") or [])
5    if "worker.py" in cmdline:
6        print(proc.info["pid"], cmdline)

This avoids false positives from generic executable names.

Common Pitfalls

The biggest mistake is checking by a generic process name when multiple unrelated processes share that executable. python alone is rarely specific enough.

Another common issue is trusting a PID file without verifying the PID still exists. A stale PID file does not prove the service is still alive.

People also misuse grep and accidentally match the grep command itself. If you use ps, prefer the bracket trick or use pgrep instead.

Finally, do not use raw process checks when a service manager, container runtime, or orchestrator already owns the lifecycle. Service-aware checks are usually more meaningful.

Summary

  • Use pgrep for quick name-based checks on Unix-like systems.
  • Use kill -0 when you already know the PID.
  • Use tasklist or Get-Process on Windows.
  • Use psutil for cross-platform checks from Python.
  • Prefer service-aware tools such as systemctl when the process is managed by a supervisor.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.