Skip to content

How to Find and Kill Processes on Linux

DodaTech 3 min read

In this tutorial, you'll learn about How to Find and Kill Processes on Linux. We cover key concepts, practical examples, and best practices.

The Problem

An application is frozen, a port is occupied, or a rogue process is consuming 100% CPU. You need to find the process and terminate it without rebooting the server.

Quick Fix

Step 1: Find the process with ps

List all running processes with their PIDs:

ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root      1234  0.0  0.1  12345  6789 ?        Ss   10:00   0:00 /usr/sbin/sshd
user      5678 99.0  2.0  23456 12345 ?        R    10:05   5:00 node /app/server.js

Step 2: Find a process by name

Search for a process by name:

pgrep -la nginx
1234 nginx: master process /usr/sbin/nginx
5678 nginx: worker process

Or use pgrep with a pattern:

pgrep -f "node server"
5678

Step 3: Find the process using a specific port

Identify what is listening on port 3000:

sudo lsof -i :3000
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
node    5678  user   11u  IPv4 12345      0t0  TCP *:3000 (LISTEN)

Or use ss:

sudo ss -tlnp | grep :3000

Step 4: Kill a process by PID

Terminate the process gracefully:

kill 5678

If the process ignores SIGTERM, force kill:

kill -9 5678

Step 5: Kill by name with pkill

Terminate all processes matching a name:

pkill -f "node server"

Use -9 for force kill:

pkill -9 -f "node server"

Step 6: Kill all instances of a program

Kill every process named firefox:

killall firefox

Step 7: Handle zombie processes

Zombie processes (shown as Z in ps aux) cannot be killed. They are already dead but their parent process has not called wait(). Kill the parent process instead:

ps aux | grep -w Z
kill 1234  # parent PID

Alternative Solutions

Use htop for interactive process management

Navigate processes with arrow keys and kill with F9:

htop

Use systemctl for service processes

Stop services managed by systemd instead of killing PIDs:

sudo systemctl stop nginx

Common Mistakes to Avoid

Using kill -9 as the first resort. SIGKILL does not let the process clean up. Use kill (SIGTERM) first and escalate to kill -9 only if necessary.

Killing the wrong process. Verify the PID with ps aux | grep or pgrep -la before killing. A wrong PID can crash unrelated services.

Forgetting that pkill matches partial names. pkill node kills all processes with 'node' in their name. Use pgrep -x for exact match.

Pro Tips

Use systemctl kill for service processes. For services managed by systemd, use systemctl kill --signal=SIGTERM service-name instead of raw kill.

Use timeout command for runaway processes. Set a maximum runtime: timeout 30 python long_script.py kills the process after 30 seconds automatically.

Use ulimit to prevent resource hogging. Set limits with ulimit -u 100 -m 512000 to limit the number of processes and memory usage per user.

Prevention

  • Use kill (SIGTERM) before kill -9 (SIGKILL) to give processes a chance to clean up.
  • Monitor runaway processes with top or htop to catch issues early.
  • Set resource limits with ulimit to prevent processes from consuming all system resources.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro