NodexVPS
Deploy VPS
Linux Guides5 min read

How to Kill a Process in Linux

Find a process by name, port or resource use, choose the right signal, and stop it with kill, pkill, killall or systemctl, even when it will not die.

NodexVPS Team
A process list on a server with one process being stopped

A program stops responding, a port is still in use after you closed the app, or a script runs away with the CPU. On Linux you end it by sending the process a signal. This guide shows how to find the right process, which signal to send, and what to do when a process will not die.

Step 1: Find the Process

Every process has a number, its process ID (PID). There are several ways to find it.

By name

pgrep -a nginx

pgrep -a lists matching processes with their PID and full command line. The classic alternative is ps aux | grep nginx, which also shows the user, CPU and memory use.

By resource use

top

Press P to sort by CPU or M to sort by memory; the PID is in the first column. htop, if installed, is easier to read and lets you send signals from the list.

By the port it uses

When an error says a port is already in use, find who holds it:

sudo ss -ltnp 'sport = :8080'
# or
sudo lsof -i :8080

The output includes the program name and its PID.

Step 2: Understand Signals

"Killing" a process really means sending it a signal. The most useful ones:

SignalNumberWhat it does
SIGTERM15Asks the process to stop. It can save its work and close files first. The default.
SIGINT2The same as pressing Ctrl+C in a terminal.
SIGHUP1Many services reload their configuration when they receive it.
SIGKILL9Ends the process immediately. It cannot be caught or ignored, so nothing is cleaned up.
SIGSTOP / SIGCONT19 / 18Pause a process and let it continue later.

Run kill -l to list every signal your system supports.

Always try SIGTERM first. SIGKILL can leave half-written files, stale lock files and broken database transactions behind. Use it only when a process ignores SIGTERM.

Step 3: Stop the Process

kill: by PID

kill 4321          # sends SIGTERM
kill -9 4321       # sends SIGKILL if it did not stop
kill -HUP 4321     # asks a service to reload

A good habit: send SIGTERM, wait a few seconds, check with ps -p 4321, and only then use -9.

pkill: by name or pattern

pkill node                       # every process named node
pkill -f "python3 worker.py"     # match the full command line
pkill -u alice                   # every process of user alice

Check what will match before you run it: pgrep -a -f "python3 worker.py" shows exactly the processes pkill -f would hit.

killall: by exact name

killall firefox

killall matches the exact program name. It comes from the psmisc package, which some minimal server images do not include.

From top or htop

In top, press k, type the PID and the signal. In htop, select the process and press F9.

Stopping Services the Right Way

Web servers, databases and most background services on a server are managed by systemd. If you kill one of them directly, systemd may start it again straight away. Use systemd instead:

sudo systemctl stop nginx
sudo systemctl restart nginx
sudo systemctl kill --signal=SIGKILL nginx   # last resort for a hung service

For containers, use docker stop <name>, which sends SIGTERM and then SIGKILL after a timeout, or docker kill <name> to stop at once.

When a Process Will Not Die

Check the process state first:

ps -o pid,ppid,stat,cmd -p 4321
  • Z (zombie): the process has already ended; only its entry remains until its parent collects it. You cannot kill a zombie. Restart or stop the parent, shown in the PPID column, and the zombie disappears.
  • D (uninterruptible sleep): the process is waiting for the kernel, usually for disk or network storage. Even SIGKILL waits until that operation finishes. Look for a hung mount or a failing disk; in the worst case a reboot is needed.
  • "Operation not permitted": the process belongs to another user. Use sudo, and make sure you have the right PID.

When the Kernel Kills for You

If a server runs out of memory, the kernel's out-of-memory killer ends a process to keep the system alive. If an application disappears without a trace, look for it:

sudo dmesg -T | grep -i -E "killed process|out of memory"
sudo journalctl -k | grep -i oom

Repeated OOM kills mean the workload needs more memory than the server has. Reduce what the application uses, add swap as a short-term buffer, or move to a cloud VPS with more RAM.

Safety Rules

  • Never kill PID 1: it is the system's init process, and the server stops working.
  • Double-check patterns before using pkill or killall as root.
  • Prefer systemctl for services and docker stop for containers.
  • If a process keeps hanging, read its logs before killing it again: the cause is usually there.

Quick Reference

TaskCommand
Find a process by namepgrep -a name
Find what uses a portsudo ss -ltnp 'sport = :PORT'
Ask a process to stopkill PID
Force it to stopkill -9 PID
Stop by namepkill name
Stop a servicesudo systemctl stop name
Check a process's stateps -o pid,stat,cmd -p PID

Setting up a new server? Our Ubuntu VPS setup guide covers users, SSH and the firewall.

Recent Insights

Start Your NodexVPS Today

Save 50% with yearly billing.