How To Force Disconnect A Login Session That Is No Longer In Use

Usually when I connect to a remote server via SSH, I am very fond of opening 2 or more connections to that server on several terminal windows. Often I close the terminal window before doing a proper `logout` from the SSH session. In most cases, the SSH connection should disconnect and the corresponding process will no longer be there. But in other instances that process will continue to run on the remote server. This has happened to me several times.

There are 2 ways that I have found out to force disconnect (or Kill) the unused SSH sessions on the remote server.

Before anything else, know where you’re at so that you will not end up disconnecting your current active session. Use `tty` command to know on which tty you are working on. The command will give you something like /dev/pts/0.

Next is to know which are sessions by listing it down using the `who` command. Or simple press “w” to get a list like the one below.

USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
user2     pts/0    19:24    8.00s  0.05s  0.05s -bash
user2     pts/1    19:25    0.00s  0.09s  0.04s w

(1) The first is with a 2-step approach. Use the following `ps` command to get the process Id of the said session. Then you can use `kill` to terminate the process.

ps -dN  | grep  pts/0

The result will be similar to this:

3948 pts/0    00:00:00 bash

Terminate the process with:

kill -9 3948

(2) The second way is a more direct approach using `pkill`.

pkill -9 -t pts/0

The above will immediately kill the said process that is associated to pts/0.

If you want to play it safe, I suggest you use #1.

Similar Posts:

Notice: This article was published on October 15, 2013 and the content above may be out of date.