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.
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.
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:
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.
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:
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:
PowerShell:
For a boolean-style PowerShell check:
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.
If you know the PID instead:
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:
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.
Or in Python:
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
pgrepfor quick name-based checks on Unix-like systems. - Use
kill -0when you already know the PID. - Use
tasklistorGet-Processon Windows. - Use
psutilfor cross-platform checks from Python. - Prefer service-aware tools such as
systemctlwhen the process is managed by a supervisor.
Related reading
- how can i launch the kafka scheduler using marathon in minimesos?
- How can I list all available charts under a helm repo?
- How can I list the taints on Kubernetes nodes?
- How can I log each request/response using Alamofire?
- How can I log SQL statements in Spring Boot?
- How can I log SQL statements in Spring Boot?
- How can I make a timeout for a crushed server
- How can I measure the actual memory usage of an application or process?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.