Skip to content
Tutorials Center
My learning

Topics

Lesson 49 of 95 ยท Processes and the System

kill, Signals and Process Control

Control Linux processes: kill and kill -l, SIGTERM versus SIGKILL, pgrep and pkill, process groups, nice and renice, ulimit, zombies and orphans.

Starting processes is easy. The harder skill is controlling them: asking a program to stop politely, forcing it to stop when it will not listen, finding it by name when you do not know its PID, giving a heavy job lower priority, and putting limits on what a process may consume. All of these are done with a small group of commands that you will use for the rest of your Linux life.

This lesson covers kill and the signals it sends, the difference between SIGTERM and SIGKILL, pgrep and pkill, process groups, nice and renice, ulimit, and the two odd process states you will hear about: zombies and orphans. Signals from the script side (trap) are in Signals and trap; this lesson is about the command line.

kill sends signals, not necessarily "kills"

The name is misleading. kill is a command (and a Bash builtin) that sends a signal to a process. Most signals do end the process by default, but the choice is yours, and a program may catch the signal and do something else.

TEXT
   you:  kill -TERM 4821
              |
              v   (kernel delivers signal 15 to process 4821)
   +-----------------------------+
   | process 4821                |
   |   default action: exit      |   or its own handler: save state, then exit
   +-----------------------------+

List the signals your system knows:

Bash Names and numbers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The signals that matter day to day:

Signal Number Meaning Can be caught?
SIGTERM 15 "Please terminate." The default for kill. Yes
SIGINT 2 Interrupt, what Ctrl-C sends. Yes
SIGHUP 1 Terminal closed; many daemons re-read their config. Yes
SIGQUIT 3 Quit and dump core (Ctrl-\). Yes
SIGKILL 9 Terminate immediately. The kernel does it, the program never sees it. No
SIGSTOP 19 Freeze the process. No
SIGCONT 18 Continue a stopped process. Yes
SIGUSR1 / SIGUSR2 10 / 12 Free for the program to define. Yes

You may write a signal as a name (kill -TERM, kill -s TERM, kill -SIGTERM) or as a number (kill -15). Names are clearer and portable; numbers differ on some other Unix systems.

SIGTERM first, SIGKILL last

The difference between the two famous signals decides how you should stop things:

  • SIGTERM (15) asks the program to stop. A well-written program catches it, finishes the current write, removes its temporary and lock files, closes connections and exits. This is the default of kill PID.
  • SIGKILL (9) is handled by the kernel. The process gets no chance to clean up: files may be half written, lock files stay, child processes are left behind. Use it only when TERM has failed.

The professional routine is: send TERM, wait a few seconds, check whether the process is gone, and only then send KILL.

Bash A polite process versus a stubborn one

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

kill -0 PID sends no signal at all. It only checks whether the process exists and you may signal it: the status is 0 if so. That makes it the standard way to ask "is it still alive?".

kill -9 is a last resort, not a habit. Servers killed with -9 can leave corrupted data or stale lock files, and the process's children keep running. If you reach for -9 first every time, you never learn why TERM did not work.

What you may signal, and kill's own output

You can only signal your own processes (same user ID); the superuser can signal anything. Trying to signal something else, or something that does not exist, fails with a clear message:

Bash kill errors (fails on purpose)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The first message is kill: (999999) - No such process; the second fails because PID 1 belongs to another user (Operation not permitted). In a script, guard against races between "check" and "kill" by accepting that kill may fail, for example kill "$pid" 2>/dev/null || true.

Finding processes by name: pgrep and pkill

Often you do not know the PID, only the program's name. pgrep prints the PIDs of processes that match a pattern; pkill sends a signal to them. Both use the same matching options:

Option Effect
(none) Pattern is a regular expression matched against the process name
-x The whole name must match exactly
-f Match against the full command line, not only the name
-u USER Only processes of that user
-n / -o Only the newest / oldest match
-c Print a count instead of PIDs
-l Print the name next to the PID

To make a demo that is safe and predictable, we copy the sleep program under a distinctive name. The process name that pgrep and ps show comes from the program file, so the copy shows up as napper.

Bash pgrep and pkill on a named process

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

pgrep exits with 0 when at least one process matched and 1 when none did, so you can use it directly in if pgrep -x nginx >/dev/null; then ....

Without -x, a pattern is a substring regular expression. pkill java would also hit javascript-helper, and pkill sh would hit bash and ssh. Always use -x for exact names, and run pgrep -l pattern first to see what you would be killing.

Process groups: stopping a whole pipeline

A command like a | b | c, or a script that starts helpers, is not one process but several. Linux keeps related processes together in a process group; a group has its own ID (the PGID, equal to the PID of its leader). A negative PID in kill means "send to every member of this group". This is how Ctrl-C stops a whole pipeline: the terminal sends SIGINT to the entire foreground group.

TEXT
   process group 5000
   +---------------------------------------+
   |  bash script (leader, PID 5000)       |
   |    +-- sleep 60   (PID 5001)          |
   |    +-- sleep 60   (PID 5002)          |
   +---------------------------------------+

   kill -TERM 5000      -> only the leader
   kill -TERM -- -5000  -> the leader and both sleeps

In a script, you can start a command in its own new group with setsid, then signal the whole group by the negative number. (Interactive shells do this automatically for every job; non-interactive shells keep everything in the script's group.)

Bash Killing a whole process group

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

If you had signalled only the leader, the two sleep 60 children would have survived as orphans. Two details: the -- tells kill that what follows is not an option (otherwise -5000 looks like a signal number), and pgrep -P PID lists the children of a process.

nice and renice: being fair to others

When several processes want the CPU, the kernel scheduler shares it out. The niceness of a process is a value from -20 (greedy, high priority) to 19 (very nice, low priority); the default is 0. A nicer process gives way to others. Any user can make a process nicer; only root can make it less nice.

  • nice -n 10 command starts a command with niceness +10.
  • renice -n 5 -p PID changes the niceness of a running process.
  • nice with no command prints the current niceness.
Bash Starting and adjusting niceness

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Lowering the priority of a process is not allowed to be undone by an ordinary user, which is why the last renice is refused. Without the 2>/dev/null it prints renice: failed to set priority for PID (process ID): Permission denied, with the real PID. Typical uses of nice: a big backup, a compression job, or a long compilation that should not slow down the web server running on the same machine. For disk-heavy work the sibling tool is ionice, which sets I/O priority (not available in every container).

ulimit: resource limits

Every process has resource limits, set by the kernel, which protect the system from runaway programs: maximum open files, maximum processes, maximum file size, maximum CPU time, and so on. The shell builtin ulimit shows and changes them for the shell and its children.

Bash Reading and lowering limits

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Each limit has two values: the soft limit is what is enforced right now, and the hard limit is the ceiling up to which an ordinary user may raise the soft limit. The writer was stopped by the kernel after exactly 1,024 bytes: status 153 is 128 + 25, signal SIGXFSZ ("file size limit exceeded"). The sandbox's default is 30,000 blocks, as the parent still shows. ulimit -f 1 lowered the soft and the hard limit together, so the later attempt to raise it is refused with cannot modify limit: Operation not permitted: lowering a hard limit is a one-way street. Because limits are inherited, setting them in a subshell, as above, keeps the change local. Some useful options: -n open files, -u user processes, -f maximum file size, -t CPU seconds, -v virtual memory, -c core file size, and ulimit -a for the list. On servers, permanent limits are configured in /etc/security/limits.conf or in the systemd unit file.

Zombies and orphans

Two process states are the subject of many interview questions and, occasionally, real problems.

Zombie (state Z). When a process ends, the kernel keeps a tiny record of it (its exit status) until the parent calls wait to read it. Between the death and the wait, the child is a zombie: it uses no memory or CPU, only a slot in the process table. You cannot kill a zombie, because it is already dead; you fix it by making the parent collect it (or by ending the parent, after which init adopts and reaps the zombie). Lots of zombies mean a buggy parent that never waits.

Orphan. A process whose parent has died. The kernel re-parents it to PID 1 (init or systemd, or a subreaper), which waits for it when it ends. Orphans are normal: that is how daemons are made, and why a script's background jobs keep running if the script is killed.

Bash Making and observing a zombie and an orphan

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The first block creates a real zombie: the ps line shows the state Z for the finished sleep 0.1, which stays in the process table until its parent exits (one second later). Ordinary Bash scripts rarely produce zombies because Bash collects its children as soon as they end; they come from programs that forget to. The second block shows an orphan: when its parent exits, the kernel gives the child a new parent. On a normal system that is PID 1 (init or systemd); inside a container it may be another designated reaper process, so the number differs, which is why the script only checks that it has changed. If you ever see zombies in ps aux (marked <defunct>), look at the parent's PID in the PPID column, because that is the program to fix or restart.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does a plain kill 4821 send?

    The default signal of kill is SIGTERM (15), a polite request to terminate that the program can catch and handle. SIGKILL has to be requested with -9 or -KILL.
  2. 2. Why can a program not clean up after kill -9?

    SIGKILL (and SIGSTOP) are handled by the kernel itself, so a program never runs a handler for them and has no chance to remove lock files or flush buffers.
  3. 3. What is kill -0 PID used for?

    Signal 0 is never delivered. kill only does the permission and existence check, and reports the result in its exit status.
  4. 4. You run pkill sh on a server to stop a script called sh. What is the danger?

    pkill and pgrep treat the argument as a regular expression matched anywhere in the process name. Use -x for an exact match and run pgrep -l first to see the matches.
  5. 5. A process is in state Z (zombie). What is the right way to get rid of it?

    A zombie has already exited and only waits for its parent to read the exit status. Signals cannot affect it; fix or end the parent, and init adopts and reaps what is left.

Summary

  • kill sends a signal; the default is SIGTERM (15), a polite request. SIGKILL (9) and SIGSTOP cannot be caught.
  • Stop programs with TERM first, check with kill -0 PID, and use KILL only as the last step.
  • pgrep finds PIDs and pkill signals them; always use -x for an exact name and check with pgrep -l first.
  • kill -- -PGID signals a whole process group, such as every process of a pipeline or script.
  • nice -n N cmd starts a command at a lower priority, renice changes a running one; ordinary users may only increase niceness.
  • ulimit shows and sets resource limits (soft and hard); set them in a subshell to keep the change local.
  • Zombies are finished children whose parent has not called wait; orphans are processes whose parent died and which init adopts.

Next: Environment, Profiles and Configuration