Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 12System · Linux

How to kill a process that hangs

A script hangs, an app does not answer, a port is still busy after a crash. The safe way is not kill -9 at once: first ask the process to stop (so it can close files and connections), then force it only if needed. This how-to shows how to find the right PID, the signals you need, how to stop services and containers the right way, and what to do with processes that "can not be killed".

  • 5 Lessen
  • 12 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

12.1

The quick answer

pgrep -a -f backup.sh
4311 /bin/bash /usr/local/bin/backup.sh
kill 4311
sleep 5; ps -p 4311 || echo stopped
kill -9 4311

kill PID sends SIGTERM (15, "please stop"). Use kill -9 (SIGKILL, "stop now") only when SIGTERM did not work.

12.2

Step by step

  1. Find the exact process. Check the full command line, the user and the start time — do not kill the wrong one:
pgrep -a -f 'java.*shop-api'
ps -o pid,ppid,user,stat,etime,cmd -p 4311
    PID    PPID USER     STAT     ELAPSED CMD
   4311       1 app      S       02:13:08 /bin/bash /usr/local/bin/backup.sh
  1. Is it a service or a container? Then stop it the managed way — otherwise systemd or Docker starts it again at once:
systemctl status 4311
systemctl restart shop-api
systemctl kill --signal=SIGKILL shop-api
docker stop -t 30 web
docker kill web
  1. Send the right signal:
SignalNumberUse
SIGTERM15Default of kill: stop cleanly. Always try this first.
SIGINT2Like Ctrl+C.
SIGHUP1Many daemons reload their config (nginx, sshd, rsyslog).
SIGKILL9The kernel stops it at once. No clean-up, temp files stay, data can be lost.
SIGSTOP / SIGCONT19 / 18Pause and continue (good for "what is this doing?").
  1. Kill by name or by port (careful — it can match more than you think; test with pgrep first):
pgrep -l -u alice python
pkill -u alice -f 'python report.py'
fuser -v 8080/tcp
fuser -k -TERM 8080/tcp
  1. Kill a whole tree (a script that started children): kill the process group, or let systemd do it for a service:
ps -o pid,pgid,cmd --forest -g 4311
kill -TERM -- -4311

The minus before the number means "the process group 4311".

  1. Stop it from running too long next time:
timeout 30m /usr/local/bin/backup.sh
timeout -k 10s 5m ./job.sh

12.3

Check that it worked

ps -p 4311 || echo "gone"
ss -ltnp | grep :8080

No line for the PID and the port is free. For a service: systemctl status shop-api shows active (running) with a new PID after the restart.

12.4

Common problems

ProblemCauseFix
kill -9 does nothing, state D in psThe process waits for the kernel (disk, NFS). It can not be killed until the I/O ends.Fix the storage/NFS problem; sometimes only a reboot helps.
Process shows <defunct> (state Z)A zombie: already dead, the parent did not collect it.It uses no CPU/RAM. Restart or kill the parent (ps -o ppid= -p PID).
It comes back right after killsystemd (Restart=), Docker (--restart), or Kubernetes restarts it.Stop the service / container / scale the deployment instead.
Operation not permittedIt belongs to another user.Use sudo, after you checked it is the right process.
pkill killed too muchThe pattern matched other commands too.Always run pgrep -a -f PATTERN first; use exact patterns or -x.
Port still busy after the killA child process holds it, or the socket is in TIME_WAIT.ss -ltnp / fuser -v PORT/tcp; TIME_WAIT ends by itself.

12.5

Go further

1Why should you try kill PID before kill -9 PID?

SIGTERM lets the program close files and connections cleanly; SIGKILL stops it at once and can lose data.

2A process is in state D and kill -9 does not work. Why?

It waits for I/O inside the kernel (often disk or NFS); it can only end when that I/O ends.

Alle commando's in dit hoofdstuk