Kill zombie sshd shells

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

0 comments

  1. v|nce

    completely usless :o)

  2. v|nce

    ca merde là !?

  3. RaiL-FleX

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

  4. Ozh

    (c'est vince le useless !)