Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: timeout problem in ssh sessions



On Feb 15, 11:32am, Andy Spiegl wrote:
} Subject: Re: timeout problem in ssh sessions
}
} Thanks to Vincent and Stephane I came up with the following .zlogout

Some suggestions follow.

} ---------------------.-----------------.------------.-----------------
} echo "Terminating shell (PID $$) on TTY `tty|cut -dy -f2`:"

No need for tty|cut:

echo "Terminating shell (PID $$) on TTY ${TTY#*y}:"

} ps -eo user,pid,ppid,s,cpu,pmem,rss,vsize,bsdstart,etime,bsdtime,cmd | /bin/grep -v "etime,bsdtime,cmd" | /bin/grep -iE "(^USER|$$)" | /bin/grep -v "grep -iE"

Why do you need ignore-case in that grep?  Surely you want to match "USER"
case-sensitively?  However, you probably DO want to match the PID as a word
by itself, otherwise e.g. "8654" would match "28654".

/bin/grep -wE "(^USER|$$)" =(ps -eo user,pid,ppid,s,cpu,pmem,rss,vsize,bsdstart,etime,bsdtime,cmd)

Better still, since you're about to do the same ps|grep on the PPID:

ps="$(ps -eo user,pid,ppid,s,cpu,pmem,rss,vsize,bsdstart,etime,bsdtime,cmd)"

echo "Terminating shell (PID $$) on TTY ${TTY#*y}:"
/bin/grep -wE "(^USER|$$)" <<<$ps
echo ""
echo Parent process (PPID $PPID):
/bin/grep -wE "(^USER|$PPID)" <<<$ps
echo ""

For the die-hards in the crowd, yes, the grep could be replaced with:

print -l ${(M)${(f)ps}:#(USER*|* $$ *)}

} echo -n "Still open file descriptors: "
} perl -e 'print join(" ", grep { -t $_ } 0..63)."\n"'
} echo ""

That's only telling you which descriptors are still connected to the tty,
not which ones are open, so you'll miss X connections and the like.

If that's really what you want, though, you don't need perl:

echo -n "Still open file descriptors: "
for fd in {0..63}; [[ -t $fd ]] && echo -n "$fd "; echo ""
echo ""

} if whence xsel > /dev/null 2>&1; then

No need for this test:  If it's not in the ps output, you're not going
to do anything with it, and if it is in the ps output, it must exist,
right?

}   LEFTOVER=`ps -eo user,pid,ppid,s,cpu,pmem,rss,vsize,bsdstart,etime,bsdtime,cmd | /bin/grep -v "etime,bsdtime,cmd" | /bin/grep -iE xsel | /bin/grep -v "grep -iE"`

If you take my suggestion above, you've already got $ps:

LEFTOVER="$( /bin/grep -wE xsel <<<$ps )"

}   if [[ -n $LEFTOVER ]]; then
}     echo 'Leftover "xsel" process:'
}     echo $LEFTOVER
} 
}     # kill xsel (started by .zsh/functions/mouse.support) if in a ssh session
}     # otherwise sshd will hang until the X clipboard is read or cleared
}     if [[ -z ${OSTYPE:#linux*} && -n $SSH_CLIENT &&
}        ${(M)${(f)"$(</proc/$PPID/status)"}:#Name:*} == Name:[[:blank:]]sshd ]]; then
}       echo -n "Terminating xsel..."
}       xsel -c -b
}       echo -e "done.\n"
} 
}       # or kill the parent sshd (pretty brutal, might close other programs!)
}       # kill -HUP $PPID 2>/dev/null
}     fi
}   fi
} fi
} 
} echo 'Last user was: '`whoami`'.  Bye!'
} ---------------------.-----------------.------------.-----------------



Messages sorted by: Reverse Date, Date, Thread, Author