View Full Version : stop remote connections during sleep

24th August 2016, 05:24 PM
I do a lot of things with ssh and sftp, now vpn too.

If your machine sleeps, these things time out rudely and hang terminal sessions, and vpn can do worse causing problems with the windowing system (not understood, but observed).

In any case, I have a little bash script that kills ssh, sftp, and vpnc if I forget and put my machine to sleep.

I call it sleepWrap - here is the source - hope it helps others

# needs to be installed: /usr/lib/systemd/system-sleep (no links work)

DATE=`date +%Y_%m_%d_%H_%M_%S`
NAME=`basename $0`


ABORT() { echo "$NAME at $DATE ERROR: $*" | tee -a $LOGFILE; exit 1; }

if [ $# -ne 2 ]; then ABORT "expect 2 parameters, got $#"
elif [ "$2" -ne "suspend" ]; then ABORT "second parameter not 'suspend', got $2"
elif [ "$1" -eq "post"]; then exit 0
elif [ "$1" -ne "pre"]; then ABORT "first parameter not 'pre' or 'post', got $1"

PIDS=`ps -eaf | egrep "ssh|sftp|vpnc" | egrep -v "grep|sshd|agent|Agent" | awk '{print $2}'`
for PID in $PIDS; do kill -1 $PID; done
exit 0