Monday, April 23, 2012

Linux / UNIX: Kill User Session

How do I kill all users session under Linux or UNIX operating system using command prompt?

You need to use the pkill command which will look up or signal processes based on name. It can send the specified signal (such as KILL) to each process. Open a command-line terminal (select Applications > Accessories > Terminal), and then type the following commands as root user:
$ sudo pkill -9 -u username
OR
# pkill -9 -u username
To list all users pids, enter:
$ pgrep -u username
OR better try:
$ ps -fp $(pgrep -d, -u userNameHere)

Example: Kill Unix / Linux User Session

In this example, list all process owned by a user called lighttpd, enter:
# ps -fp $(pgrep -d, -u lighttpd)
Sample outputs:
UID        PID  PPID  C STIME TTY          TIME CMD
lighttpd 4703 1 0 04:20 ? 00:01:07 /usr/sbin/lighttpd -f /user/local/etc/.myconf/lighttpd/master.example.com.conf
lighttpd 4705 4703 0 04:20 ? 00:00:00 /usr/bin/php-cgi
lighttpd 4708 4703 0 04:20 ? 00:00:00 /usr/bin/php-cgi
lighttpd 4710 4703 0 04:20 ? 00:00:00 /usr/bin/php-cgi
lighttpd 4712 4703 0 04:20 ? 00:00:00 /usr/bin/php-cgi
lighttpd 4714 4703 0 04:20 ? 00:00:00 /usr/bin/php-cgi
lighttpd 4715 4703 0 04:20 ? 00:00:00 /usr/bin/php-cgi
lighttpd 4716 4710 0 04:20 ? 00:00:07 /usr/bin/php-cgi
lighttpd 4718 4705 0 04:20 ? 00:00:00 /usr/bin/php-cgi
lighttpd 4719 4708 0 04:20 ? 00:00:02 /usr/bin/php-cgi
To kill all process owned by lighttpd user, enter:
# pkill -9 -u lighttpd
Please note that above command will also logout lighttpd user.

Getting help

Anytime you need assistance with Linux / UNIX pkill command-line, turn to the man page first. It will give you detailed information, parameters and switches for pkill command. For example, man pkill opens the man page for the pkill command:
$ man pkill

No comments:

Post a Comment