In: , ,
On: 2004 / 09 / 05
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

  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. v|nce says:

    completely usless :o)

  2. v|nce says:

    ca merde là !?

  3. RaiL-FleX says:

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

  4. Ozh says:

    (c'est vince le useless !)

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
Gravatars: Curious about the little images next to each commenter's name ? Go to Gravatar and sign for a free account
Spam: Various spam plugins may be activated. I'll put pins in a Voodoo doll if you spam me.

Read more ?