Linux processes and signals (ps, kill, SIGTERM, SIGKILL)
A process is a running program with a PID, and you control it by sending signals with kill: SIGTERM asks it to stop cleanly, SIGKILL forces it, and SIGHUP usually asks it to reload.
- Updated Nov 27, 2026
- Markdown
Find a process, stop it with the right signal, and tell when systemd will restart it so you stop the service instead of the process.
- An Ubuntu or Debian server you can reach over SSH
- A user with sudo access
Something on your server is using all the CPU, or a stuck program will not let go of a port. To deal with it you need to find the process, pick the right way to stop it, and know whether a supervisor will start it again. The wrong move, such as kill -9 on a database, can corrupt data.
How it works#
Every running program is a process with a PID (process ID), an owner, and a parent. Process 1 is init, which on Ubuntu is systemd, and it is the ancestor of everything else.
Processes are controlled with signals, small messages the kernel delivers. A program can register a handler to react to most of them, for example to flush files before exiting. The signals you use:
SIGTERM(15): ask the program to terminate. It can clean up. This is the default forkill.SIGKILL(9): the kernel ends the process immediately. It cannot be caught or ignored, so no cleanup runs.SIGHUP(1): originally “terminal hung up”. Many daemons treat it as “reload your config”.SIGINT(2): what Ctrl+C sends.SIGSTOPandSIGCONT: pause and resume a process.
A daemon managed by systemd is different: systemd itself is the parent, and it can restart the process when it dies. That is why killing the process often does nothing useful.
Step 1: List and find processes#
ps aux
ps aux --sort=-%mem | head
ps -ef --forest
pgrep -a nginx
pgrep -af "node server.js"ps aux shows every process with its owner (USER), PID, CPU and memory percentages, state (STAT) and command. --sort=-%mem puts the biggest memory users first. ps -ef --forest draws the parent and child tree. pgrep -a name prints matching PIDs and command lines. -f matches the whole command line, which you need for scripts started as node server.js, because the process name is only node.
Avoid ps aux | grep name as your first choice, because it also matches the grep itself. pgrep does not.
For a live view, run top, or htop if you installed it with sudo apt install htop. In top, press M to sort by memory, P for CPU, k to send a signal to a PID, and q to quit. The STAT column is worth knowing: R running, S sleeping, D waiting on disk (cannot be killed until it returns), Z zombie, T stopped.
Step 2: Stop a process with kill#
kill 2210
sleep 5
pgrep -a node
kill -9 2210kill PID sends SIGTERM. Wait a few seconds, because a program may take time to close connections. Check with pgrep. Only if it is still there, send kill -9 PID. kill -9 risks leaving lock files, half-written data and unreleased ports, so it is the last resort.
To signal by name, use pkill name (SIGTERM) or pkill -f "node server.js". It matches every process with that name, so run pgrep -a name first and read the list. A process owned by another user needs sudo kill PID.
To ask for a reload:
sudo kill -HUP "$(cat /run/nginx.pid)"For a systemd service, use sudo systemctl reload nginx instead. It runs the right reload action for you.
Step 3: Stop a managed service the right way#
Check whether systemd owns the process:
systemctl status myappRead Active:, Main PID: and the recent log lines. If the process is a service, stop and start it through systemd:
sudo systemctl stop myapp
sudo systemctl restart myapp
sudo systemctl disable myappstop ends it now, restart stops then starts it, and disable stops it from starting at boot. If you kill the process directly and the unit has Restart=always or Restart=on-failure, systemd starts a new one after RestartSec. You will see a new PID in systemctl status. The full unit file story is in run a service with systemd. Docker has the same behavior with restart policies, see fix a Docker container that keeps restarting.
To see why a service died, read its log with journalctl -u myapp -n 50, covered in read logs with journalctl.
Step 4: Handle a stuck process#
When SIGTERM does nothing:
- Check the state with
ps -o pid,stat,wchan:20,cmd -p PID. ADstate means it is waiting on I/O and even SIGKILL will wait. - Wait and retry. Many shutdowns take a few seconds, especially for apps that drain connections. See Node SIGTERM and graceful shutdown.
- Send
kill -9 PIDonly if you accept the data risk. - A zombie (
Z) is already dead and waits for its parent to collect it. Killing it does nothing. Restart the parent.
Check it worked#
pgrep -a myapp
systemctl is-active myapp
sudo ss -ltnp | grep 8080An empty pgrep and inactive mean the process is gone and stays gone. If a new PID appears after a few seconds, a supervisor restarted it. If the port is still listed, another process holds it, see find what is using a port.
Edge cases and gotchas#
- kill -9 skips cleanup. Databases, caches and file writers can lose data or leave lock files. Always try SIGTERM first.
- PIDs are reused. A PID from an hour ago may now belong to something else. Check with
ps -p PIDbefore you kill. - A kill by name hits everything.
pkill pythonends every Python process you own, including ones you forgot. List first withpgrep -a. - Killing the shell’s job. A process started with
&ornohupsurvives logout, but one started in a plain session may get SIGHUP when SSH drops. Use systemd ortmuxfor long jobs. - Containers have their own PID view. On the host,
psshows container processes too, but stop them withdocker stop, which sends SIGTERM and then SIGKILL after a timeout. - sudo is needed for other users’ processes.
kill: (1234): Operation not permittedmeans you do not own it.
Troubleshooting#
| Symptom | Likely cause | Fix |
|---|---|---|
bash: kill: (1234) - Operation not permitted | The process belongs to another user | Use sudo kill 1234 |
bash: kill: (1234) - No such process | The PID already exited or was reused | Run pgrep -a name again for the current PID |
| Process returns with a new PID after kill | systemd or Docker restarts it | sudo systemctl stop name or docker stop name |
kill did nothing and the process is still running | The program ignores or is slow to handle SIGTERM | Wait, check STAT, then kill -9 as a last step |
Process shows STAT of D and will not die | Stuck in uninterruptible I/O, often a dead disk or mount | Fix the I/O cause; reboot if needed |
Z or <defunct> entries in ps | Zombie whose parent never reaped it | Restart or fix the parent process |
pkill: killing pid 2210 failed: Operation not permitted | Not your process | Run with sudo |
Failed to stop myapp.service: Unit myapp.service not loaded. | Wrong unit name | List with systemctl list-units --type=service |
Quick reference#
ps aux --sort=-%mem | head # top memory users
pgrep -a name # find PIDs
top # live view, q to quit
kill PID # SIGTERM, clean stop
kill -9 PID # SIGKILL, last resort
kill -HUP PID # ask for reload
pkill -f "pattern" # signal by command line
systemctl status name # state and main PID
sudo systemctl stop name # stop a managed serviceRelated lessons#
- Run a service with systemd: write the unit file that does the supervising.
- Check disk, memory and CPU usage: find out why a process is hungry.
- Fix out of memory kills (OOM): when the kernel sends SIGKILL for you.
- Docker restart policies: the container version of a supervised process.
Next#
Processes run as users. See who owns what in users, groups and sudo.
Try it: Find a process and stop it cleanly
Find a stray process, ask it to stop with SIGTERM, then check how a managed service reports its state.
Want a real machine? Free browser labs: Killercoda
Check what you learned
Frequently asked questions
What is the difference between kill and kill -9?
kill sends SIGTERM, which lets a program clean up and exit. kill -9 sends SIGKILL, which the kernel enforces at once and the program cannot catch, so it may leave temp files, locks or half-written data.
How do I find the PID of a process in Linux?
Run pgrep -a name to list matching PIDs with their command lines, or ps aux | grep name. For a systemd service, systemctl status name shows the main PID.
Why does a process restart after I kill it?
Something supervises it, usually systemd with Restart=always or a Docker restart policy. Stop it through the supervisor, for example sudo systemctl stop name.
How do I reload a service config without restarting it?
Many daemons reload on SIGHUP, which systemctl reload name sends or runs the right command for. Check that the service supports reload before you rely on it.
Sources
Keep going: guides and tutorials
- Install a Self-Hosted PaaS on Your Own Server in Five Minutes One installer download, one setup token, one sample app. The full Levelrail install on a fresh Linux server, with the checks and the fixes.
- Manage Secrets Self-Hosted: Env Vars and Master Key Rotation Set env vars and encrypted secrets on a self-hosted server, apply changes safely, and rotate the master key with no downtime without locking yourself out.