Processes and jobs

Linux commands: kill

Learn how the Linux kill command sends signals to a process by PID, defaulting to TERM, and how to send others like KILL, HUP, or STOP, by name or number.

Linux processes can receive signals and react to them. A signal is a short message from the operating system, or from another process, that says “something happened”.

That’s one way we can interact with running programs.

The kill command sends signals to a process. Despite the name, it’s not only used to terminate a program. But that’s its main job, and the reason you’ll type it.

We use it in this way:

kill <PID>

By default, this sends the TERM signal to the process with that process ID.

Let’s try it on a process we don’t mind losing. Start one in the background, then kill it with the PID the shell printed:

sleep 300 &
[1] 51234
kill 51234
[1]+  Terminated              sleep 300

The Terminated line is the shell telling us the job ended because of a signal. You find PIDs with ps, top, or jobs -l.

We can use flags to send other signals, including:

kill -HUP <PID>
kill -INT <PID>
kill -KILL <PID>
kill -TERM <PID>
kill -CONT <PID>
kill -STOP <PID>

HUP means hang up. The system sends it automatically when the terminal window that started a process is closed before the process ends.

INT means interrupt. It’s the same signal you send when you press ctrl-C in the terminal, and it usually terminates the process.

KILL is not delivered to the process. It goes to the operating system kernel, which immediately stops and terminates the process. The program gets no chance to save its work or clean up.

TERM means terminate. The process receives it and terminates itself, closing files and connections on the way out. It’s the default signal sent by kill, and the one you should try first.

CONT means continue. It resumes a stopped process.

STOP is also handled by the kernel, not the process. It immediately stops (but does not terminate) the process, which you can later resume with CONT.

My habit is kill <PID>, wait a couple of seconds, check with ps. Only if the process is still there do I use kill -KILL <PID>. A program that ignores TERM is usually stuck, and KILL is the way out.

You might see numbers used instead of names, like kill -1 <PID>. In this case:

1 corresponds to HUP. 2 corresponds to INT. 9 corresponds to KILL. 15 corresponds to TERM. 18 corresponds to CONT on Linux (19 on macOS). 19 corresponds to STOP on Linux (17 on macOS).

The first four are the same everywhere. CONT and STOP differ between Linux and macOS, so I always use the names. kill -l prints the full list your system uses.

If you get the PID wrong, kill tells you:

kill 99999
bash: kill: (99999) - No such process

Either the process already exited, or you copied the wrong number. Run ps again and check.

This command works on Linux, macOS, WSL, and anywhere you have a UNIX environment

Lesson completed