List a user's sessions and kill them (Linux, AIX)
ps, pgrep, pkill, who and ss commands to list and terminate a user's processes and SSH connections.
Commands to list the processes and SSH sessions of a user, and to end them, on Linux and AIX.
List and kill the processes of a user
ps -fp $(pgrep -d, -u app_user)
pkill -9 -u app_user
pkill -9 ends every process of the user immediately, without cleanup. List the processes first.
Find the terminal of the current session
tty
End a session process gracefully
kill -HUP <pid> # or: kill -1 <pid>
SIGHUP tells the processes attached to a terminal that the terminal has been closed.
Process list of a user on Linux and AIX
# Linux
ps -L u n | tr -s " " | cut -d " " -f 2,3,14-
# AIX
ps -u monitor | cut -d " " -f 3 > pid.lst
kill -9 `cat pid.lst`
The AIX example writes the PIDs to a file and kills them all with kill -9, without any check.
SSH sessions
ps -fp $(pgrep -d, -u dba)
who -a
ps ax | grep sshd
netstat -natu | grep 'ESTABLISHED'
netstat -tnpa | grep 'ESTABLISHED.*sshd'
ss | grep ssh
ss is the modern replacement for netstat.