{"id":97,"date":"2004-09-05T20:36:49","date_gmt":"2004-09-05T18:36:49","guid":{"rendered":"http:\/\/frenchfragfactory.net\/ozh\/archives\/2004\/09\/05\/kill-zombie-sshd-shells\/"},"modified":"2007-05-09T23:37:10","modified_gmt":"2007-05-09T21:37:10","slug":"kill-zombie-sshd-shells","status":"publish","type":"post","link":"https:\/\/planetozh.com\/blog\/2004\/09\/kill-zombie-sshd-shells\/","title":{"rendered":"Kill zombie sshd shells"},"content":{"rendered":"<p>When I&#39;m connected to my host shell via ssh and let the shell idle for too long, it disconnects from the remote server, but the sshd session does not die and it sometimes ends up with several zombie sshd &quot;running&quot;.<\/p>\n<p>So I&#39;ve created this bash function to kill those sshd zombies but not my current sshd session<br \/>\n<!--more--><\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-1\"><pre class=\"language-none line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-none\">function nozombie {\r\nCOUNT=0;\r\nfor PID in `ps x | grep -v grep | grep sshd | awk &#039;{print $1}&#039; | grep -v &quot;PID&quot;` ;\r\ndo\r\n        if [ &quot;$PID&quot; != &quot;$PPID&quot; ] ; then\r\n                kill -9 $PID;\r\n                let &quot;COUNT = $COUNT +1&quot;;\r\n        fi;\r\ndone\r\n        if [ &quot;$COUNT&quot; &gt; 0 ] ; then\r\n                echo &quot;killed $COUNT zombie sshd !&quot;;\r\n        else\r\n                echo &quot;No shell killed.&quot;;\r\n        fi;\r\n}<\/code><\/pre><\/div>\n<p>Newbie explanations : the function collects a list of process id corresponding to &quot;sshd&quot;, compares each PID with the current sshd PID, and kills it if it doesn&#39;t match. The interesting variable here is <strong>$PPID<\/strong>, which is PID of parent of current process (current process is bash, whose PID is <strong>$$<\/strong>)<\/p>\n<p>Add this to your <em>~\/.bashrc <\/em>or <em>~\/.bash_profile<\/em>, depending on your configuration. To use it, simply type <em>nozombie <\/em>at the prompt once you have relogged (or re-source the file).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Bash function to kill zombie sshd<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[5,2,3],"class_list":["post-97","post","type-post","status-publish","format-standard","hentry","tag-bash","tag-code","tag-linux"],"_links":{"self":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/97","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/comments?post=97"}],"version-history":[{"count":0,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/97\/revisions"}],"wp:attachment":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/media?parent=97"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/categories?post=97"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/tags?post=97"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}