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
- function nozombie {
- COUNT=0;
- for PID in `ps x | grep -v grep | grep sshd | awk '{print $1}' | grep -v "PID"` ;
- do
- if [ "$PID" != "$PPID" ] ; then
- kill -9 $PID;
- let "COUNT = $COUNT +1";
- fi;
- done
- if [ "$COUNT" > 0 ] ; then
- echo "killed $COUNT zombie sshd !";
- else
- echo "No shell killed.";
- fi;
- }
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
completely usless :o)
ca merde là !?
A piece of code 's never useless, it may be adapted to another situation … or usefull for someone else !
(c'est vince le useless !)