Respect of the Kill: How To Properly Terminate Your Linux Processes

Every day, Linux users misuse the kill utility when trying to terminate a process. You go and type the famous kill -9, and all the problems are solved. But should you do that?

tl;dr Try to send a SIGTERM by using kill -15. After a few seconds, If It is not dead send a SIGINT (kill -2). Finally, If It persists send a SIGHUP (kill -1).

Author: Daniel StoriCC BY-NC-SA 4.0

The kill Command

kill is a shell builtin in most Bourne-derived shells such as Bash and Zsh. Let’s man it.

kill(1) - Linux man pageName
kill - terminate a process
Synopsis
kill [-s signal|-p] [ - ] pid…
kill -l [signal]
Description
The command kill sends the specified signal to the specified process or process group. If no signal is specified, the TERM signal is sent. The TERM signal will kill processes which do not catch this signal. For other processes, it may be necessary to use the KILL (9) signal, since this signal cannot be caught.
Most modern shells have a builtin kill function, with a usage rather similar to that of the command described here. The '-a' and '-p' options, and the possibility to specify pids by command name is a local extension.
If sig is 0, then no signal is sent, but error checking is still performed.

More at kill(1) — Linux man page.

Don’t Use kill -9 !

It doesn’t give the process a chance to end cleanly. Your process may need to:

  • Shut down socket connections.
  • Clean up temp files.
  • Inform its children that it is going away.
  • Reset its terminal characteristics

There are many other cleanup cases.

Image for post
Image for post
Author: Daniel StoriCC BY-NC-SA 4.0

What Should I Use Then?

Let’s look at the available options.

0 - ? 
1 - SIGHUP - ?, controlling terminal closed,
2 - SIGINT - interupt process stream, ctrl-C
3 - SIGQUIT - like ctrl-C but with a core dump, interuption by error in code, ctl-/
4 - SIGILL
5 - SIGTRAP
6 - SIGABRT
7 - SIGBUS
8 - SIGFPE
9 - SIGKILL - terminate immediately/hard kill, use when 15 doesn't work or when something disasterous might happen if process is allowed to cont., kill -9
10 - SIGUSR1
11 - SIGEGV
12 - SIGUSR2
13 - SIGPIPE
14 - SIGALRM
15 - SIGTERM - terminate whenever/soft kill, typically sends SIGHUP as well?
16 - SIGSTKFLT
17 - SIGCHLD
18 - SIGCONT - Resume process, ctrl-Z (2nd)
19 - SIGSTOP - Pause the process / free command line, ctrl-Z (1st)
20 - SIGTSTP
21 - SIGTTIN
22 - SIGTTOU
23 - SIGURG
24 - SIGXCPU
25 - SIGXFSZ
26 - SIGVTALRM
27 - SIGPROF
28 - SIGWINCH
29 - SIGIO
29 - SIGPOLL
30 - SIGPWR - shutdown, typically from unusual hardware failure
31 - SIGSYS

You got some options, my friend! To kill a process respectfully, you should:

  • Try to send a SIGTERM by using kill -15.
  • After a few seconds, If It is not dead send a SIGINT (kill -2).
  • If It persists send a SIGHUP (kill -1).

Conclusion

What if it doesn’t want to terminate at the end?

  • If you are using an external binary and it didn’t stop after all of this, you should probably get rid of it.
  • If you are developing a program and can’t get it to stop correctly, you should better handle the killing signals.

If you have no other choices, kill -9that ******.

Written by

Data Consultant. Databricks Certified Associate Developer.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store