Linux and servers · Beginner · 16 min
Manage Linux processes with ps, kill and signals
By Padding · Updated 18 Nov 2026
Ubuntu 26.04.1 LTS · procps-ng 4.0.4
You will find processes with ps and pgrep, look inside one through /proc, stop, pause and reload a small script with signals, spot zombies and orphans, change priority with nice, and keep a job alive after you log out.
On this page
Every running program on a Linux server is a process with a number (its PID), a parent, an owner and a state. When something misbehaves you need to find the right process, see what it is doing, and tell it to stop in a way that lets it tidy up first. Doing that well is mostly about signals: small numbered messages the kernel delivers to a process.
In this tutorial you write a small Bash script that runs forever and reacts to signals, then practise on it: list it with ps, find it with pgrep, read its details in /proc, and send it the six signals you will use most. You also create a zombie and an orphan on purpose, change a process's priority, and compare background jobs, nohup and a systemd service. Everything lives in ~/proc-demo, which you delete at the end.
Step 1 · Install the tools and write a demo worker
ps, pgrep, pkill and kill (the program) come from the procps package. Ubuntu server images include it. Install it if it is missing:
sudo apt-get update
sudo apt-get install -y procpsNow create a script that writes to a log, saves its PID and then loops. It traps three signals: TERM and INT make it clean up and exit, and HUP makes it log a "reload" and carry on.
mkdir -p ~/proc-demo
cd ~/proc-demo
cat > worker.sh <<'EOF'
#!/bin/bash
# worker.sh: a long-running job that reacts to signals.
cd "$(dirname "$0")"
exec 3>>worker.log # keep the log open on file descriptor 3
echo "$$ started" >&3
echo "$$" > worker.pid
stop() {
echo "$$ got $1, cleaning up" >&3
rm -f worker.pid
exit 0
}
trap 'stop TERM' TERM
trap 'stop INT' INT
trap 'echo "$$ got HUP, reloading settings" >&3' HUP
while true; do
sleep 1 &
wait $!
done
EOF
chmod +x worker.shThe loop uses sleep 1 & followed by wait. Bash runs a trap only after the current foreground command finishes, so a plain sleep 60 would delay the reaction by up to a minute. wait returns as soon as a signal arrives, so the trap runs at once.
Start it in the background:
./worker.sh &Check it worked
cat worker.logYou see one line such as 4127 started. The number is the worker's PID, and worker.pid holds the same number.
Step 2 · List processes with ps
ps with no options shows only the processes in your terminal. To see everything, pick the columns yourself with -eo and add --forest to draw the parent and child tree:
ps -eo pid,ppid,user,stat,ni,etime,cmd --forestThe columns are:
pidandppid: the process and its parent.user: who owns it, which decides who may signal it.stat: the state.Rrunning,Ssleeping (waiting for something),Dwaiting on disk or network and not interruptible,Tstopped,Zzombie. Extra letters follow:ssession leader,+in the foreground of a terminal,Nlow priority,<high priority.ni: the nice value, covered later.etime: how long it has been running.cmd: the full command line.
To find what is using the most CPU right now, sort by a column. A minus sign means largest first:
ps -eo pid,user,pcpu,pmem,rss,comm --sort=-pcpu | head -n 6rss is resident memory in KiB. You will also see ps aux in many guides. It shows a fixed set of columns and is fine for a quick look, but -eo lets you ask for exactly what you need.
Check it worked
In the forest output, find /bin/bash ./worker.sh drawn with \_ under your own bash shell, and below it a short-lived sleep 1. The worker's stat is S, because it spends almost all its time waiting.
Step 3 · Find processes by name with pgrep
Reading the whole list is slow. pgrep prints the PIDs that match. -x means the process name must match exactly, -a adds the command line, and -u limits it to one user:
pgrep -x worker.sh
pgrep -a -u "$(whoami)" -x worker.sh
pid=$(pgrep -x worker.sh)
echo "$pid"Without -f, pgrep matches the process name, which the kernel cuts to 15 characters. With -f it matches anywhere in the full command line, which is more powerful and much easier to get wrong. Start a second process that only mentions the worker in its arguments, then compare:
tail -f ~/proc-demo/worker.log > /dev/null &
tailpid=$!
pgrep -a worker
pgrep -a -f workerThe first command finds only worker.sh. The second also finds tail -f /home/<you>/proc-demo/worker.log, because the word worker is in its arguments. On a busy server the same pattern could match an editor with the file open, a log viewer, or another user's job.
pkill takes the same options as pgrep and signals every match. Before you run any pkill -f, run the same pattern through pgrep -a -f and read every line it prints. Prefer pkill -x <name> or a single PID when you can. Stop the tail by its PID:
kill "$tailpid"Check it worked
echo "$pid" printed one number, the same one in worker.pid, and pgrep -a worker printed one line.
Step 4 · Look inside a process with /proc
Every process has a directory at /proc/<pid>/. The files in it are generated by the kernel when you read them. Four are worth knowing:
readlink /proc/"$pid"/cwd
tr '\0' ' ' < /proc/"$pid"/cmdline; echo
ls -l /proc/"$pid"/fd
grep -E '^(Name|State|PPid|Threads|VmRSS)' /proc/"$pid"/statuscwdis a link to the working directory, here~/proc-demo. It answers "which folder is this app running in?"cmdlineholds the arguments separated by null bytes.trturns them into spaces.fd/lists open files. 0, 1 and 2 are standard input, output and error. 3 isworker.log, because the script opened it withexec 3>>. Bash keeps the script itself open on a high number such as 255.statusgives the name, state, parent, thread count and memory in plain text.
There is also environ, the environment the process started with. It can contain passwords and tokens, so only the owner and root can read it. The same applies to cwd and fd for other users' processes: put sudo in front, for example sudo ls -l /proc/<pid>/fd.
A deleted file that a process still holds open shows up in fd/ with (deleted) after its name. That is the usual reason df reports a full disk after you removed a large log.
Check it worked
ls -l /proc/"$pid"/fd shows a line ending in 3 -> /home/<you>/proc-demo/worker.log (or /root/... if you are root), and State: in status reads S (sleeping).
Step 5 · Send the signals that matter
kill sends a signal to a PID. Its name is misleading: it sends TERM by default, but it can send any signal. Use names rather than numbers, because the numbers for STOP and CONT differ between processor architectures. These six cover nearly all day-to-day work:
| Signal | Usual number | Sent by | A well-behaved program |
|---|---|---|---|
| HUP | 1 | kill -HUP, a closed terminal | reloads its configuration (daemons), or exits (shells and their jobs) |
| INT | 2 | Ctrl+C in a terminal | stops what it is doing and exits |
| TERM | 15 | kill, systemctl stop, shutdown | cleans up and exits |
| KILL | 9 | kill -KILL | cannot react: the kernel ends it |
| STOP | 19 on x86 and Arm | kill -STOP (Ctrl+Z sends the related TSTP) | cannot react: the kernel pauses it |
| CONT | 18 on x86 and Arm | kill -CONT, fg, bg | carries on where it stopped |
kill -l lists every signal on your system.
HUP: reload without stopping
kill -HUP "$pid"
sleep 1
tail -n 2 worker.log
ps -o pid,stat,cmd -p "$pid"The log gains got HUP, reloading settings and ps still shows the worker. Nginx, SSH and many other daemons reread their configuration on HUP in the same way.
STOP and CONT: pause and resume
kill -STOP "$pid"
sleep 1
ps -o pid,stat,cmd -p "$pid"The state is now T. A stopped process uses no CPU but keeps its memory and open files. In an interactive shell, Bash also prints a Stopped line for the job. Resume it:
kill -CONT "$pid"
sleep 1
ps -o pid,stat,cmd -p "$pid"The state is back to S.
TERM: ask it to stop
kill "$pid"
sleep 1
tail -n 1 worker.log
lsThe log ends with got TERM, cleaning up and worker.pid is gone, because the trap ran. Always try TERM first. It gives a program the chance to finish writing files, close database connections and remove lock files.
INT: what Ctrl+C sends
Start the worker again, then send INT as if you had pressed Ctrl+C while it ran in the foreground:
./worker.sh &pid=$(pgrep -x worker.sh)
kill -INT "$pid"
sleep 1
tail -n 1 worker.logThe last line reads got INT, cleaning up.
KILL: the last resort
Some programs ignore TERM, or are stuck and never get round to handling it. Write one that ignores it on purpose:
cat > stubborn.sh <<'EOF'
#!/bin/bash
# stubborn.sh: ignores TERM, so only KILL stops it.
cd "$(dirname "$0")"
trap '' TERM
echo "$$" > stubborn.pid
while true; do sleep 1; done
EOF
chmod +x stubborn.sh
./stubborn.sh &spid=$(pgrep -x stubborn.sh)
kill "$spid"
sleep 2
ps -o pid,stat,cmd -p "$spid"It is still running. Now send KILL, which no program can catch, ignore or delay:
kill -KILL "$spid"sleep 1
pgrep -x stubborn.sh || echo "stubborn.sh is gone"
lsstubborn.pid is still in the folder. KILL gives no chance to clean up, so half-written files, stale PID files and lock files are left behind. Use it only after TERM has had a few seconds to work.
Check it worked
worker.log holds, in order, started, got HUP, got TERM, a second started and got INT. pgrep -x stubborn.sh found nothing, and stubborn.pid remains as the sign of an unclean exit.
Step 6 · Spot zombies and orphans
When a process exits, it stays in the process table as a zombie until its parent reads its exit status with wait. Normally that takes microseconds. A parent with a bug that never calls wait collects zombies. Make one: the outer bash starts sleep 1, then replaces itself with sleep 60, which never waits for its child.
bash -c 'sleep 1 & exec sleep 60' &
parent=$!
sleep 2
ps -o pid,ppid,stat,cmd --ppid "$parent"You see [sleep] <defunct> with state Z and the parent's PID in the PPID column.
A zombie uses no CPU or memory, only a slot in the process table, and you cannot kill it because it is already dead. The fix is the parent. Stop the parent, and the zombie passes to PID 1 (systemd on a server), which reaps it at once:
kill "$parent"
sleep 1
ps -eo pid,ppid,stat,cmd | awk '$3 ~ /^Z/'The awk filter prints every zombie on the system. On a healthy server it prints nothing. A handful that come and go is normal. Hundreds with the same parent mean that parent has a bug: restart it.
An orphan is the opposite case: a running process whose parent has exited. Here bash -c starts a sleep in the background and exits straight away:
bash -c 'sleep 120 > /dev/null 2>&1 & echo $! > ~/proc-demo/orphan.pid'
ps -o pid,ppid,stat,cmd -p "$(cat ~/proc-demo/orphan.pid)"The orphan keeps running, and its PPID is now 1, or the PID of a subreaper if a process above your shell has registered as one. Orphans are harmless in themselves; this is how daemons used to detach from a terminal. Stop it:
kill "$(cat ~/proc-demo/orphan.pid)"Check it worked
You saw a Z line under a live parent, the awk filter printed no zombie with that parent after you stopped it, and the orphaned sleep 120 reported a different parent from the shell that started it.
Step 7 · Change priority with nice and renice
When processes compete for CPU, the scheduler favours those with a lower nice value. The range is -20 (most favoured) to 19 (least), and new processes start at 0. Start a job at 10, which suits backups, compression and report builds:
nice -n 10 sleep 300 &
npid=$!
ps -o pid,ni,cmd -p "$npid"Any user may make their own process nicer (a higher number):
renice 15 -p "$npid"
ps -o pid,ni,cmd -p "$npid"Going back down, to a lower number, needs root. Without sudo, renice 5 -p "$npid" fails with Permission denied. With it:
sudo renice 5 -p "$npid"
ps -o pid,ni,cmd -p "$npid"
kill "$npid"Nice values only matter when the CPU is busy. On an idle server a niced job runs at full speed. They do not limit disk use; for that, look at ionice or the I/O settings of a systemd service.
Check it worked
The NI column read 10, then 15, then 5.
Step 8 · Keep a job running after you log out
When an SSH connection closes, the login shell receives HUP and passes it on to every job it started, and those jobs usually exit. If you manage servers over a mobile or patchy connection, this is how a long import dies halfway when the link drops. There are three ways to avoid it, from the weakest to the most robust.
A background job, then disown
& runs a command in the background, and jobs lists what this shell started:
sleep 600 &
bgpid=$!
jobs -l
disown -h "$bgpid"disown -h tells your shell not to forward HUP to that job when it exits. The process itself still dies if anything else sends it HUP:
kill -HUP "$bgpid"
sleep 1
ps -o pid,stat,cmd -p "$bgpid" || echo "the disowned sleep is gone"nohup
nohup starts a command with HUP ignored, whoever sends it. If you do not redirect output, it writes to nohup.out in the current directory.
nohup sleep 600 > /dev/null 2>&1 &
hpid=$!
sleep 1
kill -HUP "$hpid"
sleep 1
ps -o pid,stat,cmd -p "$hpid"This one survives HUP. The first sleep 1 gives nohup a moment to start and set HUP to ignored; a signal sent in that instant would still stop it. TERM and KILL still stop it.
A systemd service
nohup keeps a job alive through a logout, but not through a crash or a reboot, and nothing restarts it. For anything that should always be running (an API, a worker, a bot), write a systemd service instead. systemd starts it at boot, restarts it on failure, sends it TERM on stop and then KILL if it does not exit in time, and keeps its output in the journal. The full walk-through is in Run an app as a systemd service.
A rule of thumb:
- A command for the next few minutes, while you watch:
&andjobs. - A one-off long task that must survive a dropped connection:
nohup, or run it insidetmux. - Anything that should run unattended: a systemd service.
Check it worked
The disowned sleep was gone after HUP, and ps still listed the nohup one.
Step 9 · Clean up
Stop the last background process:
kill "$hpid"The next command deletes ~/proc-demo and everything in it, including the scripts and the log. Check the path before you press Enter:
cd ~
rm -rf ~/proc-demoCheck it worked
pgrep -a -f proc-demo prints nothing and ls ~/proc-demo reports No such file or directory.
Next steps
- Use these tools to track down load, memory and disk pressure in Find why a Linux server is slow.
- Turn a long-running script into a managed service in Run an app as a systemd service.
- Write traps that clean up reliably in your own scripts with Write safer Bash scripts with strict mode and traps.
Related tutorials
- Write safer Bash scripts with strict mode and trapsYou will build a small Bash release script that stops on the first error, says which line failed, survives file names with spaces and always removes its temporary files.
- Write least-privilege sudo rules with visudoYou will replace blanket sudo access with checked rule files in /etc/sudoers.d that let two operators restart one service and edit one file, give an automation account a password-free command list, and audit what every account can run.
- Upgrade a server from Ubuntu 24.04 LTS to 26.04 LTSYou will take a backed-up Ubuntu 24.04 server through do-release-upgrade to 26.04 LTS over SSH with a fallback login, then confirm services, repositories and language runtimes work on the new release.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials