How-to

Kill a Process on Linux, Gently First

Killing a process is sending it a signal. Ask politely first, force it only when needed, and let systemd handle services it manages.

How do I find the process?

pgrep -a node
pgrep -af 'queue:work --queue=mail'
ps -eo pid,ppid,user,stat,etime,cmd --sort=-etime | grep -i [r]eport

pgrep -a prints PIDs with their command lines; -f matches against the full command, which helps when many processes share a name. The bracket trick in the last line stops grep from matching itself.

Is it a service?

If systemd started it, use systemd. Killing a service's main process directly often makes systemd restart it at once, which looks like the kill "did not work":

systemctl status php8.3-fpm --no-pager | head -5
systemctl try-reload-or-restart php8.3-fpm
systemctl kill --signal=SIGUSR2 php8.3-fpm

systemctl stop or restart runs the unit's own shutdown logic and handles every child process. systemctl kill sends a signal to all processes of the unit, useful for reload signals such as USR2. Find which unit owns a PID with systemctl status 4812.

Which signals matter?

SignalNumberMeaning
TERM15Please exit; the default for kill
INT2Interrupt, as from Ctrl+C
HUP1Many daemons reload config on it
KILL9Stop now; cannot be caught or ignored
STOP / CONT19 / 18Pause and resume

TERM lets a program finish writes, close files, release locks and remove temporary data. KILL gives it no chance, which can leave half-written files, stale lock files and, for databases, a longer recovery on the next start.

How do I send them?

kill 4812                 # TERM
sleep 5; kill -0 4812 && kill -KILL 4812
kill -HUP 1290            # reload, for daemons that support it

kill -0 sends no signal; it only tests whether the process still exists, which makes the "TERM, wait, then KILL" pattern scriptable. You can signal your own processes; other users' processes need root or sudo.

When should I use pkill or killall?

When you mean every process matching a name or pattern, and you have checked what matches. Preview with pgrep, then act:

pgrep -af 'python3 export_' && pkill -f 'python3 export_'
pkill -u builder -TERM

The second line ends every process of the builder account, handy before deleting a user. Be careful with killall: on Linux it matches names, but on some other Unix systems it kills everything, so habits from one system can hurt on another.

Why will a process not die even with KILL?

Two common reasons:

  • It is a zombie (state Z). It already exited; only an entry remains until its parent collects the exit status. You cannot kill a zombie; restart or fix the parent, shown in the PPID column.
  • It is in uninterruptible sleep (state D). It waits on the kernel, usually storage or a hung network mount, and signals are held until the wait ends. Find the cause: cat /proc/4812/stack as root, dmesg for I/O errors, or a dead NFS server.

If a D-state process never returns and blocks important work, a reboot clears it. On our machines that is a panel action or systemctl reboot, and the box is back in a few minutes.

How do I keep a runaway process from returning?

Find who starts it: ps -o ppid= -p 4812 gives the parent, and walking up usually ends at a systemd unit, a cron job or a container. Fix it there. For memory hogs, limit the service with MemoryMax= as described in Linux memory management; for CPU hogs, CPUQuota= in the unit caps usage. Monitoring tools are covered in The top command.

Keep Reading

The top command

Read every line of top, use the keys that matter, save a layout and run it in batch mode for logs and scripts.

Open

Kill a MySQL query or connection

Find runaway MySQL or MariaDB queries in the processlist, end the query or the whole connection, and set time limits so they stop themselves.

Open

Linux memory management

Virtual memory, pages, overcommit, huge pages, cgroup limits and the OOM killer: how Linux hands out RAM and the knobs worth turning.

Open

Check CPU usage and load

Read load averages against your thread count, split CPU time by type with mpstat and vmstat, find the busy process and keep history with sar.

Open

FAQ

Quick Answers

Not quite. Plain kill sends TERM, asking the process to exit cleanly. kill -9 sends KILL, which stops it immediately without cleanup.

A service manager or parent restarts it. Stop the systemd unit or fix whatever launches it.

Preview with pgrep -af, then use pkill with the same pattern.

You cannot; it already exited. Restart or fix its parent so the entry is collected.

It is waiting on the kernel, usually storage. Signals take effect only when that wait ends.