In: , ,
On: 2004 / 09 / 05 Viewed: 47683 times
Shorter URL for this post: http://ozh.in/22

When I'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 "running".

So I've created this bash function to kill those sshd zombies but not my current sshd session

CODE:
  1. function nozombie {
  2. COUNT=0;
  3. for PID in `ps x | grep -v grep | grep sshd | awk '{print $1}' | grep -v "PID"` ;
  4. do
  5.         if [ "$PID" != "$PPID" ] ; then
  6.                 kill -9 $PID;
  7.                 let "COUNT = $COUNT +1";
  8.         fi;
  9. done
  10.         if [ "$COUNT"> 0 ] ; then
  11.                 echo "killed $COUNT zombie sshd !";
  12.         else
  13.                 echo "No shell killed.";
  14.         fi;
  15. }

Newbie explanations : the function collects a list of process id corresponding to "sshd", compares each PID with the current sshd PID, and kills it if it doesn't match. The interesting variable here is $PPID, which is PID of parent of current process (current process is bash, whose PID is $$)

Add this to your ~/.bashrc or ~/.bash_profile, depending on your configuration. To use it, simply type nozombie at the prompt once you have relogged (or re-source the file).

Shorter URL

Want to share or tweet this post? Please use this short URL: http://ozh.in/22

Metastuff

This entry "Kill zombie sshd shells" was posted on 05/09/2004 at 8:36 pm and is tagged with , ,
Watch this discussion : Comments RSS 2.0.

No Comment yet

  1. 1
    v|nce France »
    replied, on 09/Sep/04 at 2:51 pm # :

    completely usless :o)

  2. 2
    v|nce France »
    thought, on 09/Sep/04 at 2:52 pm # :

    ca merde là !?

  3. 3
    RaiL-FleX Peru »
    commented, on 09/Sep/04 at 8:52 pm # :

    A piece of code 's never useless, it may be adapted to another situation ... or usefull for someone else !

  4. 4
    Ozh France »
    said, on 09/Sep/04 at 10:32 pm # :

    (c'est vince le useless !)

Leave a Reply

Comment Guidelines or Die

  • HTML: You can use these tags: <a href=""> <em> <i> <b> <strong> <blockquote>
  • Posting code: Post raw code (no <> &lt; etc) within appropriate tags : [php][/php], [css][/css], [html][/html], [js][/js], [sql][/sql], [xml][/xml], or generic [code][code]
  • Gravatars: Curious about the little images next to each commenter's name ? Go to Gravatar.
  • Spam: Various spam plugins on patrol. I'll put pins in a Voodoo doll if you spam me.
  • I will mark as Spam test comments, all comments with SEO names (ie "My Cool Online Shop" instead of "Joe") or containing forum-like signatures.

Read more ?