Linux Process Management from Zero: ps, kill, jobs, and Three Exit Codes
Bash 5.2.21, ps from procps-ng 4.0.4, zero packages: 3 sleep jobs start; 2 left after SIGTERM with wait exit code 143, 1 left after SIGKILL with wait exit code 137; timeout cuts a 30-second sleep off after 2 seconds with exit code 124; 0 left at the end.

On disk, a program is just a pile of files; once it runs, it is a process. Sooner or later everyone who writes code has to deal with processes: a background job that wandered off somewhere, a script that is stuck and will not say where, one process eating all the CPU with no name attached, an old process that was never killed blocking a new deploy. The toolkit for managing processes has three pieces: ps for looking, kill for stopping, jobs for managing background work. This guide covers all three in one pass, and every number in it was actually run on this machine.
First, the demo environment, so you can reproduce the numbers. This machine runs GNU bash 5.2.21, and ps comes from procps-ng 4.0.4. The demo script is demo255_g2.sh, with zero third-party packages; every process in the demo is a sleep command started as a child of the script, and all of them are cleaned up before the script exits — 0 child sleep processes remain at the end, nothing left behind. The sleeps are deliberately long, 300 seconds, 120 seconds, and 60 seconds, so there is something to kill. Process IDs differ on every run, so this guide quotes only counts and exit codes, never specific PIDs; the numbers in the Chinese and English transcripts agree exactly, and reproducing takes two commands, bash demo255_g2.sh --lang zh and bash demo255_g2.sh --lang en, both exiting 0.
ps is the eye for looking at processes, so learn to read its output first. Section 0 of the demo prints this shell's own facts: state S, nice value 0. The five columns you need are PID, the process ID, a unique identity for every process; PPID, the parent process ID, whoever started it; STAT, the state, a single letter; NI, the nice value that controls priority; and CMD, the command that started it. Four STAT letters matter: R is running, S is sleeping and waiting for something to do, T is stopped, Z is a zombie — already dead, but the parent has not collected it yet. The sleeps in the demo spend most of their time in S, and turn to T when stopped. By default ps shows only processes related to the current terminal; add aux or ef to see the whole system. For clean numbers the demo looks only at this shell's children, with ps --ppid followed by this shell's process ID.
A background job starts with an & sign. Put & after a command and the shell sends it to the background and hands the prompt straight back, so you can type the next command; without &, the command occupies the terminal in the foreground until it finishes. Section 1 of the demo starts 3 sleep 300 jobs in the background, one after another, each one a job. The Bash manual states it plainly: the shell numbers each job starting at 1, and the jobs command prints that table; when Bash starts a job asynchronously it also prints the process ID of the last process in the job, which is the value $! holds and the value kill needs. A job and a process are different things: one job can be a whole pipeline of processes, but in the demo one job is one sleep process, so counting jobs and counting processes gives the same number.
You do not have to point at background jobs by process ID every time; the shell offers a shorter spelling called a jobspec. The Bash manual spells it out: job numbers start at 1, %1 is job 1, %+ is the current job, %- is the previous job, %ce is a job whose command starts with ce, and %% means the same as %+. So kill %1 kills job 1, fg %1 brings job 1 into the foreground, bg %1 resumes a stopped job 1 in the background, and typing %1 on its own is the same as fg %1. Jobspecs are short, memorable, and hard to mistype by one digit; their drawback is that they only work inside the shell that started the job — switch terminals and they point nowhere. The demo script uses process IDs, because the script needs wait to ask for exit codes, and a jobspec cannot answer that; in a terminal, managing jobs by hand, the jobspec is the smoother tool. Use each where it fits.
Once jobs are started, you have to be able to find them. Section 2 of the demo cross-checks with two commands: pgrep -P with this shell's process ID counts 3 child sleep processes, and ps --ppid with this shell's process ID also matches 3 lines. pgrep is the tool for finding process IDs by condition: -P finds only the children of one parent, -f matches against the whole command line, and a follow-up check outside the demo also tried pgrep -f by name. ps --ppid has a small trap: ps itself is a child of this shell, so it counts itself in its own output; the demo script writes the output to a temporary file first and counts the sleep lines with grep to get a clean 3. When finding processes, prefer pgrep: the sharper the condition, the cleaner the match; ps is better for the details once you have found them — state, priority, and command all at once.
The first choice for stopping a process is always SIGTERM, which is a kill with no signal number. kill sends signal 15, SIGTERM, by default, and it means please exit: the process can clean up before it goes — close files, write logs, release locks — and end gracefully. Section 3 kills one sleep 300 this way: the child sleep count drops from 3 to 2, and wait reports exit code 143. Take 143 apart: 128 plus 15, where 128 is the base for died by a signal and 15 is the number of SIGTERM. wait is the shell builtin that waits for a job; after a job ends, asking wait for its exit code gives 128 plus the signal number for a job killed by a signal. Remember 143 and you have remembered a rule: whenever you see exit code 143, the process was killed by SIGTERM, no need to dig through logs to guess.
Signals form a table, and memorizing the common numbers is enough. Checked on this machine with kill -l: 1 is SIGHUP, sent when the terminal disconnects; 2 is SIGINT, the one Ctrl+C sends; 9 is SIGKILL, which cannot be caught; 15 is SIGTERM, the kill default; 18 is SIGCONT, resume running; 19 is SIGSTOP, freeze the process. kill -l with no arguments prints the whole table, signal numbers next to names, so kill -9 and kill -KILL mean the same thing, and kill -TERM means the same as a bare kill. Ctrl+C is, at its core, a SIGINT sent to the foreground process group — the same effect as kill -2, one sent by the keyboard and one by a command. Knowing this table lets you read a cause of death backwards from an exit code: 143 is 128 plus 15, 137 is 128 plus 9, and 130 is 128 plus 2, which means the process was interrupted by Ctrl+C.
The second choice is SIGKILL, or kill -9. Section 4 kills a second sleep 300, the child sleep count drops from 2 to 1, and wait reports exit code 137, which is 128 plus 9, where 9 is the number of SIGKILL. The difference between SIGKILL and SIGTERM is not force but negotiation: SIGTERM is a request the process can catch, clean up after, or even refuse; SIGKILL is an execution, the process never even learns about it, the kernel kills it outright, and not one piece of cleanup happens. So the order is always TERM first, then KILL: send TERM, wait a moment, confirm the process really left, and only then send KILL to one that is still hanging around. The demo puts the two exit codes side by side, 143 and 137, so you can tell them apart at a glance. And one rule belongs in permanent memory: confirm the process ID with pgrep before killing, because process IDs are different on every start, killing from memory easily kills the wrong neighbor, and there is no undo.
Pausing and resuming use another pair of signals, SIGSTOP and SIGCONT. Section 5 starts a sleep 120, sends kill -STOP, and ps shows the state change from S to T, where T means stopped; then kill -CONT moves it back from T to S, and the process goes on sleeping as if nothing happened. STOP differs from KILL in being reversible: STOP only freezes, CONT thaws, and the process's memory and open files stay exactly where they were. The jobs table moves along with it: while one job is stopped, the jobs table holds 2 entries, and after cleaning up the stopped job it holds 1 — the table only keeps jobs that are still alive, and the shell removes dead ones. Ctrl+Z with fg and bg is the same machinery used interactively: press Ctrl+Z on a foreground command to stop it, then bg to send it to the background or fg to pull it back to the foreground. The demo script runs in a non-interactive environment, so this part follows the Bash manual and was not measured on this machine — try it in your own terminal first.
A zombie process is the Z in ps output, worth one paragraph of its own so the first sighting is not alarming. After a child process dies, the parent must call wait to collect its exit code; until then, the child's body stays in the process table with state Z. In the demo, every killed sleep was followed immediately by a wait — that is where exit codes 143 and 137 came from — so the demo ends with no zombie leftovers and a clean 0 child sleep processes. A zombie holds no memory and no CPU, only one slot in the process table, and the harm is that enough of them can fill up the process IDs. The cure is not killing the zombie — it is already dead and cannot die twice — but fixing the parent: make the parent wait on time, or hand the child to a parent that collects its dead. The demo deliberately created no zombie to show off; the letter Z is from the manual, and when you meet one, investigate the parent.
nice controls priority, and smaller numbers mean higher priority. In section 0, this shell's nice value is 0, the default; section 6 starts a sleep 60 with nice -n 10, and ps reads its nice value as 10. Nice values run from -20 to 19, with -20 the highest and 19 the lowest, so 10 is one step below the default. Ordinary users can only raise the nice value — make their own processes less preferred; lowering it, grabbing a higher priority, needs privilege. A follow-up check outside the demo tried with user nobody: nice -n -5 to start a sleep printed Permission denied, refusing the raise in priority, but the command itself still ran and exited 0 — the refusal is about priority, not about running. Adding nice -n 10 to long jobs, backups, and batch work is a cheap, useful habit: they still finish, they just step aside first when the CPU gets busy.
timeout puts a deadline on a command, guarding against one that hangs forever. Section 7 runs timeout 2 sleep 30: the sleep wanted 30 seconds, timeout allowed it 2, killed it when the 2 seconds were up, and the exit code was 124, with the measured time exactly 2 seconds. 124 is timeout's own exit code, meaning the time ran out and I acted — a different thing from 128 plus a signal number for a signal-killed process. timeout is most useful inside scripts: calling an outside service, waiting on a network, running a command that might hang — wrap it in timeout, get 124 on expiry, and the script carries on instead of being dragged to a halt by one stuck command. Pick the timeout for the real situation: too short and healthy work gets killed by mistake; too long and the protection against hangs is gone. The demo's 2 seconds against 30 seconds makes the timeout impossible to miss: a command that wanted 30 seconds was cut off after 2.
The chart shows the demo's process counts: starting from 3 sleeps, 2 after SIGTERM, 1 after SIGKILL, and 0 after cleanup; next to them, the three exit codes 143, 137, and 124.

Sometimes you do not need to kill a process, only to confirm it is still there, and kill -0 does exactly that. kill -0 sends no signal, it only asks the system whether this process ID still exists: in a follow-up check outside the demo, kill -0 against this shell exited 0, meaning alive, and against a nonexistent process ID 999999 it exited 1, meaning gone. Scripts test life and death with this trick: probe with kill -0 first, send TERM only if alive, skip it if already dead. Two more tools are worth knowing by name: pkill sends a signal to processes by name in bulk, and killall kills a whole batch by name. Both are installed on this machine, but the demo did not run them — a bulk operation that kills the wrong set is expensive, so print the list with pgrep first, check it one by one, and only then act. Find, probe, signal, re-check: four steps in a fixed order, and skipping any one of them can kill the wrong thing.
When the machine slows down, the culprit has a ready-made command: ps aux --sort=-%cpu sorts by CPU use from high to low, and the first line is the hungriest process; swap -%cpu for -%mem and it sorts by memory instead. A follow-up check outside the demo ran this command, and the %CPU, %MEM, STAT, and COMMAND columns gave the whole picture at once, with the heaviest process visible at the top. Do not rush to kill what you find: look first at what it is, who started it, and whether it is doing legitimate work — backups, builds, and test runs all spike the CPU briefly, and killing a process doing its job costs more than letting it finish. Once you are sure it is out of control, use the TERM-then-KILL order, and re-check with pgrep afterwards to confirm the count reaches 0.
When a shell exits with background jobs still running, the Bash manual has a section for that too. Exiting a shell that still has stopped jobs prints a warning first; with the checkjobs option enabled, running jobs trigger a warning and a listing as well; exit a second time right away and the shell stops warning, kills the stopped jobs, and exits. The demo script killed every sleep before exiting, so the jobs table was empty and this never came up; in daily terminal use, run jobs before exiting, and either resume or kill a stopped job instead of leaving it behind. wait with no arguments waits for all background jobs to finish, wait with a process ID waits only for that one — the demo always used wait with a process ID, asking for the exit code of the job just killed, which is where 143 and 137 came from.
Troubleshooting process problems has a fixed order too. First, see the process with ps and pgrep: ps --ppid for one shell's children, pgrep -f to find by name, confirming the process ID and state before touching anything. Second, probe with kill -0: exit code 0 means alive, 1 means gone, and a gone process needs no killing. Third, TERM first, then KILL: send TERM, wait, count with pgrep to see whether it really dropped, and only then send KILL to one that is still hanging around. Fourth, re-check afterwards: only a pgrep count of 0 means clean, and the demo ends with exactly 0 child sleep processes. Fifth, read the logs: why the process stuck, why it refused to exit — the answer is in the logs, not in the force of the kill. Getting the order wrong is expensive: opening with kill -9 gives the process no chance to write logs or release locks, leaves half-finished files behind, and the next start has to clean up the mess.
Finally, seven rules you can copy as-is. First, TERM first, then KILL — never open with kill -9. Second, confirm the process ID with pgrep before killing; process IDs differ every time, and killing from memory easily kills the wrong one. Third, add nice -n 10 to long jobs, so they step aside when the CPU gets busy and still finish. Fourth, wrap any command that might hang in timeout; a timeout returns 124 and the script is never dragged to a halt. Fifth, re-check with pgrep after killing, and only a count of 0 means clean — the demo's 0 child sleep processes at the end are the model for this habit. Sixth, keep background jobs in the jobs table: note the job number when starting, and either resume or kill a stopped job instead of leaving it alone. Seventh, never practice kill on a production server: practice on your own machine first, with a harmless command like sleep, confirming every exit code matches before touching a real service.
Walk through this checklist once and you can reproduce everything in this guide on your own machine. First, run ps on this shell and confirm state S and nice value 0 — those are this machine's values. Second, start 3 sleep 300 jobs in the background, count 3 with pgrep -P, and count 3 lines with ps --ppid. Third, kill one with no signal number and remember the child count dropping to 2 and the wait exit code 143; kill another with -9 and remember the child count dropping to 1 and the wait exit code 137. Fourth, start a sleep 120, send kill -STOP and watch the state turn to T, send kill -CONT and watch it turn back to S, and see the jobs table go from 2 entries to 1. Fifth, start a sleep 60 with nice -n 10 and read the nice value 10 from ps. Sixth, run timeout 2 sleep 30 and remember exit code 124 and 2 seconds elapsed. The demo script demo255_g2.sh and both transcripts are in the draft directory; two commands reproduce it all, bash demo255_g2.sh --lang zh and bash demo255_g2.sh --lang en. When reproducing, note that the demo script starts 3 sleep jobs on every run and cleans them all up, so no sleep process is left on the machine afterwards, and the 0 child processes at the end are the proof. What was not measured on this machine is also stated plainly: the Ctrl+Z, fg, and bg interactive switching follows the Bash manual, because the demo script runs in a non-interactive environment and did not measure it; no zombie process with state Z appeared in the demo, and the state letter is from the manual; pkill and killall are installed on this machine but the demo did not run them, so check the list with pgrep before any bulk operation; and systemd service management is outside this guide — starting and stopping services follows each service's own documentation.
Comments (0)
Sources
Related articles
tmux 3.4, zero packages: 1 session, 2 windows, 3 panes; the logs window prints 1 to 5, editor panes report ready. Detach with Ctrl-b d, reattach with tmux attach — the session outlives the connection. Mouse, prefix key and history limit in ~/.tmux.conf included.
GCC 13.3.0, C17, zero packages: a first C program, sizeof results 1/2/4/8/8/4/8/8, sum 1..100 is 5050, 10! is 3628800, 8 numbers sorted, 3 books total 1088 pages, 3 file lines and 70 chars read back. Preprocessing alone expands 3 lines to 815.
6 files, 24 lines, 4 TODO lines; false | true gives PIPESTATUS 1 and 0, pipefail on exits 1, off exits 0; a spaced filename splits into 2 words unquoted, 1 quoted; backup 6 files, 445 bytes. Bash 5.2.21, measured.