Skip to content

Instantly share code, notes, and snippets.

@Tardis07
Last active July 6, 2023 03:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tardis07/8c4f81ea94b5a46fd768daebe10a0207 to your computer and use it in GitHub Desktop.
Save Tardis07/8c4f81ea94b5a46fd768daebe10a0207 to your computer and use it in GitHub Desktop.
kill dead ssh sessions
#!/bin/bash
########
# ~ ps ax
# PID TTY STAT TIME COMMAND
# 1 ? Ss 7:05 /lib/systemd/systemd --system --deserialize 22
# 2 ? S 0:03 [kthreadd]
# 3 ? I< 0:00 [rcu_gp]
# ......
# 1787740 pts/1 S+ 0:00 grep --color=auto grep
# ~ w
# 11:39:31 up 12 days, 9:41, 15 users, load average: 0.14, 0.29, 0.27
# USER TTY 来自 LOGIN@ IDLE JCPU PCPU WHAT
# milktea tty7 :0 246月23 12days 6:28 0.15s /usr/bin/lxsession -s LXDE-pi -e LXDE -w openbox-lxde-p
# milktea tty1 - 246月23 12days 0.08s 0.07s -bash
# milktea pts/1 tmux(38850).%8 306月23 0.00s 0.51s 0.01s w
# ......
# ~ # example output of command w and ps
########
current_pts=`ps ax | grep grep | awk '{print $2}'`
echo "current pts is : $current_pts"
w | grep -v tmux | grep pts | grep -v $current_pts | awk '{print $2}' | while read line
do
echo $line
pid=`ps -ft $line | grep -v UID | awk '{print $2}'`
sudo kill -s 9 $pid
# pkill -t pts/1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment