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).

The kill Command

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

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.

What Should I Use Then?

Let’s look at the available options.

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 ******.

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