Linux › Section B · Basic commands and the directory hierarchy › Topic B.10
Listing and manipulating processes: signals and jobs
Every running program is a process with a number, and the shell gives you tools to list them, pause them, move them into the background and stop them. Doing this well means knowing which signal to send, and why the gentle one comes first.
Processes and PIDs
A process is a running instance of a program, as the topic The kernel: processes, memory, drivers, and system calls described. The kernel gives every process a numeric process ID (PID), unique among the processes running at that moment. Once a process ends, its PID can eventually be reused, so a PID you noted yesterday may now belong to something else. Commands that act on processes, such as kill, take PIDs as arguments, so the first job is to find the right number.
Listing processes with ps
Run ps on its own and it lists the processes started from your current terminal. The default columns are PID; TTY, the terminal the process is attached to; TIME, the processor time it has used so far, which is not the same as how long it has existed; and CMD, the command line. Longer formats add STAT, the state: R running or ready to run, S sleeping while waiting for an event, D uninterruptible sleep, usually waiting for disk input or output, T stopped, and Z a zombie, a process that has finished but has not yet been collected by its parent.
Useful ps options
ps accepts three option styles: Unix options with a dash, BSD options without one, and GNU long options. ps x shows all your processes, including those without a terminal; ps ax shows every process on the system; adding u, as in ps aux, gives a user-oriented format with the owner and CPU and memory percentages. The Unix-style equivalent of ps aux is ps -ef. To look at one process, give its PID, as in ps u 4182 or ps -p 4182. In the shell, the special parameter $$ holds the shell's own PID, so ps u $$ describes your shell. pgrep name prints the PIDs of processes whose names match.
Signals
A signal is a short message from the kernel to a process, sent because of an event or because another process asked for it with kill. Each signal has a name and a number and a default action, such as terminating, stopping or continuing the process. A program can install a handler to react differently, such as saving its work before exiting, or can ignore most signals. The kernel also reports faults this way: SIGSEGV for an invalid memory access, SIGPIPE for writing to a pipe nobody reads. Shell scripts can catch signals with trap, covered in Temporary files and here documents.
The signals you will use
SIGTERM (15) politely asks a process to finish and is what kill sends by default. SIGINT (2) is an interrupt, sent when you press Ctrl+C. SIGHUP (1) originally meant the terminal hung up; many daemons treat it as an instruction to reload their configuration. SIGKILL (9) ends a process immediately. SIGSTOP pauses a process, SIGTSTP is the pause request sent by Ctrl+Z, and SIGCONT resumes a paused process. SIGKILL and SIGSTOP cannot be caught, blocked or ignored. The numbers for SIGHUP, SIGINT, SIGKILL and SIGTERM are the same on every Linux architecture, but others vary, so prefer names, and run kill -l to list them on your system.
Ending processes properly
kill 4182 sends SIGTERM, giving the program a chance to finish writing files and remove temporary ones. Add a signal by name or number to send another: kill -STOP 4182 freezes the process in memory and kill -CONT 4182 resumes it. Only if a process ignores SIGTERM should you escalate to kill -KILL 4182, also written kill -9. The kernel then removes the process at once, with no clean-up, which can leave corrupt or stale lock files. systemd follows the same order when stopping a service, waiting up to 90 seconds by default before sending SIGKILL. pkill name and killall name signal processes by name. Ordinary users can signal only their own processes; anything else gives Operation not permitted.
Worked example: stopping a stuck program
A script called report.sh has stopped responding in another terminal. 1. Find it: pgrep -f report.sh prints, say, 5120; -f matches against the full command line. 2. Check it really is the right process with ps u 5120 before signalling anything. 3. Ask it to stop: kill 5120 sends SIGTERM. 4. Wait a few seconds, then test with ps -p 5120; if the process is gone, you are done. 5. If it is still there and ignoring SIGTERM, use kill -KILL 5120 as a last resort. 6. Then look for half-written output files, since SIGKILL allowed no clean-up.
Job control
The shell tracks the commands you start from it as jobs, numbered 1, 2 and so on. Press Ctrl+Z and the foreground job receives SIGTSTP and stops, and the prompt returns. jobs lists your jobs with their state; fg resumes the most recent job in the foreground; bg resumes it in the background. Refer to a particular job with a percent sign: fg %2 or kill %1. Ctrl+C sends SIGINT to every process in the foreground job, so a whole pipeline is interrupted at once. If a shell warns about stopped jobs when you exit, run jobs to see them.
Background processes
End a command with & and the shell starts it in the background, prints its job number and PID, and returns the prompt at once: gzip big.log & lets you keep working while the file compresses. The special parameter $! holds the PID of the most recent background job. Background jobs still share your terminal: their output appears in the middle of your work, and one that tries to read from the terminal is stopped with SIGTTIN until you bring it to the foreground. Redirect their input and output to files. To keep a job running after you log out, start it with nohup, or use a terminal multiplexer such as tmux.
Worked example: pausing and resuming a job
These steps were run in GNU Bash 5.2 with sleep standing in for a long task. 1. Start it in the background: sleep 300 &. The shell prints the job number and PID. 2. Pause it: kill -STOP %1. 3. Run jobs, which prints [1]+ Stopped sleep 300. 4. Resume it: kill -CONT %1, and jobs now shows [1]+ Running sleep 300 &. 5. End it: kill %1 sends SIGTERM, and the shell reports [1]+ Terminated sleep 300. The plus sign marks the current job, which fg and bg use when no job is named.
Common mistakes to avoid
First, reaching for kill -9 straight away, which denies the program any clean-up; send SIGTERM first and wait. Second, killing a PID without checking it with ps, since PIDs are reused and a typo can hit the wrong process. Third, pressing Ctrl+Z when you meant Ctrl+C, leaving stopped jobs that hold files open. Fourth, leaving a background job's output unredirected. Fifth, reading TIME in ps as the age of a process: it is CPU time used. Sixth, expecting kill to work on another user's process without root.
Key takeaways
Each process has a PID, which can be reused after it ends. ps aux and ps -ef list all processes, with STAT states such as R, S, D, T and Z. kill sends signals: SIGTERM (15) by default, SIGINT (2) from Ctrl+C, SIGHUP (1) often for reloading, and SIGKILL (9) as a last resort, which, like SIGSTOP, cannot be caught. Ctrl+Z sends SIGTSTP; jobs, fg, bg and %n manage jobs. & runs a command in the background; redirect its output, and use nohup or tmux to outlive a logout.
Flashcards
Flashcard
Process ID, the kernel's number for a running process
Test your understanding
Question 1 of 5
A process ignores kill 3307. What is the appropriate next step?
Question 2 of 5
You pressed Ctrl+Z in a running editor by mistake. What happened, and how do you get back to it?
Question 3 of 5
In ps output, what does the TIME column show?
Question 4 of 5
As an ordinary user, you run kill 1. What do you expect?
Question 5 of 5
Why should a long-running background job usually have its output redirected, as in ./sync.sh > sync.log 2>&1 &?
