{"id":254318,"date":"2021-05-20T11:00:12","date_gmt":"2021-05-20T08:00:12","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/sigint-sigterm-and-sigkill-cloudsavvy-it\/"},"modified":"2021-05-20T11:00:12","modified_gmt":"2021-05-20T08:00:12","slug":"sigint-sigterm-and-sigkill-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/sigint-sigterm-and-sigkill-cloudsavvy-it\/","title":{"rendered":"#SIGINT, SIGTERM, and SIGKILL \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a2ec5acb817e\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a2ec5acb817e\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/sigint-sigterm-and-sigkill-cloudsavvy-it\/#What_Is_a_Linux_Signal\" >What Is a Linux Signal?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/sigint-sigterm-and-sigkill-cloudsavvy-it\/#SIGKILL_SIGTERM\" >SIGKILL &amp; SIGTERM<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/sigint-sigterm-and-sigkill-cloudsavvy-it\/#Sending_Linux_Signals_with_Keyboard_Sequences\" >Sending Linux Signals with Keyboard Sequences<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/sigint-sigterm-and-sigkill-cloudsavvy-it\/#Wrapping_up\" >Wrapping up<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#SIGINT, SIGTERM, and SIGKILL \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<figure style=\"width: 1836px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage wp-image-11396 size-full\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/05\/0a8db1ac.png?width=1200&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"danger symbol\" width=\"1836\" height=\"870\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><figcaption class=\"wp-caption-text\"><span class=\"type:primaryImage imagecredit\"><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.shutterstock.com\/image-vector\/glitched-attention-danger-symbol-computer-hacked-1135176134\">Shutterstock\/SkillUp<\/a><\/span><\/figcaption><\/figure>\n<p>Software interrupts on Linux and Unix systems are made via signals. There are many different Linux signals, but a few stand out and are important to understand and know: SIGINT, SIGTERM, and SIGKILL. Here\u2019s how they work.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"What_Is_a_Linux_Signal\"><\/span>What Is a <i>Linux Signal<\/i>?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>We all understand a red light means that we have to stop walking or driving. Similarly, in Linux and Unix systems, one can pass a signal to a running program or service to interact with it. For example, one could send a red traffic light sign to a program by simply issuing a <code>SIGTERM<\/code> signal.<\/p>\n<p>Just like with a red traffic light, people may choose to continue to walk or drive when the light is red. Whilst that may not be a safe idea for all involved, a <code>SIGTERM<\/code> signal send to a process will do exactly that; the process\/program may choose to ignore such a signal.<\/p>\n<p>The basic Linux signals all have a number (1-30+). After a little while, a proficient Linux user will <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/general\/\" data-internallinksmanager029f6b8e52c=\"3\" title=\"General\" target=\"_blank\" rel=\"noopener\">general<\/a>ly know one or more of these. For example, the <code>SIGTERM<\/code> signal matches with number 15, and signal 9 (<code>SIGKILL<\/code>) is likely the most the most known one as it allows one to forcefully terminate a process, unlike our <code>SIGTERM<\/code> red light example.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-11073\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/05\/7afbb160-1.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"htop displaying possible program signals\" width=\"1016\" height=\"454\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Here we see the main screen of <code>htop<\/code> (you can install this handy utility by typing <code>sudo apt install htop<\/code> on Ubuntu\/Mint, or <code>sudo yum install htop<\/code> on RedHat\/Centos\/Fedora) with a number of termination and other signals (lower in the list; there are 37 in total) which can be sent to a process previously selected on the right. One can select a process by pressing the cursor up\/down and then send a signal by using F9.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"SIGKILL_SIGTERM\"><\/span>SIGKILL &amp; SIGTERM<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Whilst the name may sound a little sinister, the common Linux lingo for process termination is that one \u2018kills\u2019 a process. Generally speaking, we will only want to terminate a process with a <code>-9<\/code> (<code>SIGKILL<\/code>) signal if such a process\/program is hanging. Note that whenever we speak of process or program, the words can be interchanged at will. Basically, a process is any running program (or service) that has been given a <i>PID<\/i> (a process identifier).<\/p>\n<p>Let\u2019s look at an example of terminating a running background process by using a <code>SIGKILL<\/code> signal to the running process. Note that as explained <code>SIGKILL<\/code> is rather destructive and will terminate the process no matter what the process would like to do with the signal. A number of signals can be captured and redirected by the process, whereas others cannot.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-11074\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/05\/586e508f-1.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Sending SIGKILL to a sleep process running in the background\" width=\"663\" height=\"120\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Here we started a <code>sleep 1800<\/code> in the background (using <code>&amp;<\/code> at the end of the command), which was started as the first (<code>[1]<\/code>) background process with <code>PID 574660<\/code>. We subsequently killed that background process using <code>kill -9 574660<\/code>, where the <code>-9<\/code> stands for <code>SIGKILL<\/code>.<\/p>\n<p>While the process is terminated im<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/social-mediaa\/\" data-internallinksmanager029f6b8e52c=\"1\" title=\"Social Media\" target=\"_blank\" rel=\"noopener\">media<\/a>tely, we do not see the termination message (background process <code>1<\/code> killed, i.e., <code>[1]+ Killed<\/code>) as the command prompt returns before the message is displayed, i.e., returning the command line was a faster operation then the process termination, or similar.<\/p>\n<p>We check the process list by grepping for the PID <code>ps -ef | grep 574660<\/code>. Whilst there is some output, the output shown is only for our running <code>grep<\/code> command; the <code>sleep<\/code> process has already been terminated.<\/p>\n<p>Let\u2019s evaluate the same with <code>SIGTERM<\/code>, i.e. <code>kill -15 ${PID}<\/code> where <code>${PID}<\/code> is the process we want to terminate.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-11075\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/05\/59b2900a-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Sending SIGTERM to a sleep process running in the background\" width=\"379\" height=\"100\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>We had to press enter to trigger\/show the termination message (as explained above). We can see that the program terminated correctly again. However, this time, while invisible for this particular example (read on!), internally inside the <code>sleep<\/code> program code, things went a little different.<\/p>\n<p>In this case (using <code>-15<\/code> i.e. <code>SIGTERM<\/code> to terminate the process), the <code>sleep<\/code> process was notified and had the opportunity to internally handle the signal. It could subsequently respond by self-terminating, by ignoring the signal, or by any other action as developed into the code. We can also prove this to be true by checking the exit signal and\/or output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-11076\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/05\/9eb60bc8-1.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"The difference in output and exit codes depending on the signal sent\" width=\"277\" height=\"153\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Here we started the process <code>sleep 2000<\/code> twice, and then used another shell\/terminal session to terminate the program. The first time, we used <code>kill -9<\/code> and the second time we used <code>kill -15<\/code> to stop the <code>sleep<\/code> process.<\/p>\n<p>We immediately notice how the output returns <code>Killed<\/code> in the first (<code>kill -9<\/code> action) instance. For the second attempt (using <code>kill -15<\/code>), we see the output <code>Terminated<\/code> instead; the program self-terminated. Subsequent to the respective terminations we also checked the exit signals, and found that the exit codes were different.<\/p>\n<p>Why is this important? Consider larger programs; in this instance, we were just terminating a simple <code>sleep<\/code> command. There was no data being handled, no traffic being sent back\/forth, etc. But what would h<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>en if we sent a <code>kill -9<\/code> command to our database server (this would generally speaking require sudo\/root-level privileges)?<\/p>\n<p>It would trigger the database to go into crash recovery mode the next time it starts up because, as far as the database software knows, all that happened was \u201cwas there\u201d followed by \u201cnothing.\u201d In other words, it crashed. If we had instead issued a <code>kill -15<\/code> command, the database software could have done a controlled shutdown, for example, by first blocking new users from connecting, then disconnecting\/terminating existing users, then finish writing data, etc. before finally self-terminating.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Sending_Linux_Signals_with_Keyboard_Sequences\"><\/span>Sending Linux Signals with Keyboard Sequences<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Did you know that whenever you sent a <code>CTRL+c<\/code> key sequence to a running program, for example in a terminal, that instead a <code>SIGINT<\/code> is sent? Let\u2019s step back to our trusted <code>sleep<\/code> command and test this:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-11077\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/05\/c00b5755.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"A sleep process interrupted by a SIGINT signal sent via a CTRL+C key sequence\" width=\"223\" height=\"86\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Here we started <code>sleep<\/code> again, and then pressed the keyboard combination <code>CTRL+c<\/code>. The program terminated, or better <i>was interrupted<\/i> by the <code>SIGINT<\/code> signal that was sent. We request the exit code and confirm that once again the exit code is different from the previous signals.<\/p>\n<p>Note that this exit code, for sleep, will always be matched to the signal sent, though potentially not all signals may be covered. In other words, when using <code>CTRL+c<\/code> at the command line the exit code will always be <code>130<\/code>, <code>137<\/code> when killed with <code>kill -9<\/code>, and <code>143<\/code> when <code>kill -15<\/code> was used.<\/p>\n<p>You can test exit codes for commands by querying the <code>$?<\/code> variable, which holds the exit code of the previous command (as long as you have not commenced a new command). Knowing the exit code of a given command in a particular situation, and\/or as the result of a particular signal sent to that command, helps when scripting solutions that handle other processes, etc. (which is the case for many shell scripts, especially when managing servers or automated environments).<\/p>\n<p>Another often-used keyboard sequence is <code>CTRL+z<\/code>. This will sent a <code>SIGTSTP<\/code> signal, a <code>suspend<\/code> signal which immediately places a process in suspension until (for example) a <code>'fg'<\/code> command is issued for the same process which will bring it back to the foreground.<\/p>\n<p>To learn more about process management, see Bash Process Termination Hacks.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Wrapping_up\"><\/span>Wrapping up<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In this article, we looked at the most important Linux signals and how we may practically use them in a Linux or Unix environment. Knowing the basic Linux signals aids one with daily Linux use and management, for example, when a process is hanging and needs to be terminated with <code>kill -9<\/code>. In a future article, we may look at capturing a signal using the <code>trap<\/code> command from inside a Bash script, allowing one to define a custom procedure to be executed when such a signal is issued.<\/p>\n<p>If you enjoyed reading this article, have a look at our Bash automation <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/watch-movies-tv-seriess\/\" data-internallinksmanager029f6b8e52c=\"8\" title=\"Watch Movies &amp; TV Series\" target=\"_blank\" rel=\"noopener\">series<\/a> starting at Bash Automation &amp; Scripting Basics. In the third article in that series, we also discuss background process management, touched on earlier in this article.\n<\/p><\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/11072\/linux-signals-hacks-definition-and-more\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#SIGINT, SIGTERM, and SIGKILL \u2013 CloudSavvy IT&#8221; Shutterstock\/SkillUp Software interrupts on Linux and Unix systems are made via signals. There are many different Linux signals, but a few stand out and are important to understand and know: SIGINT, SIGTERM, and SIGKILL. Here\u2019s how they work. What Is a Linux Signal? We all understand a red&#8230;<\/p>\n","protected":false},"author":1,"featured_media":254319,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/05\/0a8db1ac.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-254318","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/254318","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=254318"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/254318\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/254319"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=254318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=254318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=254318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}