How to kill or terminate unwanted tty/pts sessions in Linux?

Have you ever faced this situation, a lot of terminal connections to your server?

Before starting, we have a brief discussion on TTY. The word tty stands for teletype terminals. Some years ago, user terminals were connected to computers’ electromechanical teleprinters or teletypewriters (TeleTYpewriter, TTY), since then the name TTY has continued to be used as the name for the text-only console.

Here CryBit going to explain the command-line option to kill unwanted or unused or idle ttys.

We need the PID (Process ID) of that particular terminal (tty). First check the active connections server using the command “w.” Please see the sample output pasted below:

tty

[email protected] [~]# w
 02:05:41 up 234 days, 23:46,  3 users,  load average: 1.47, 1.89, 1.98
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    w3-oc.lolipop-i   23:51   53:49   0.04s  0.04s -bash
root     pts/2    w3-oc.lolipop-i   01:11    0.00s  0.01s  0.01s w
root     pts/3    w3-oc.lolipop-i   01:12   53:32   0.00s  0.00s -bash

Here, you can see three tty connections to your server, pts/0, pts/2 and pts/3 where PTS stands for pseudo terminal. You can also see which processes are currently executing for those tty connections. In this command we could not see the process ID (PID) of those ttys.

We can use the PS command to find out the process ID. Here is the sample output:

# ps -ft tty

Example

[email protected] [~]# ps -ft pts/0
UID          PID    PPID  C STIME TTY          TIME CMD
root      331857  331761  0 Oct09 pts/0    00:00:00 -bash

Here You will get the user info and process ID. Then use kill command to terminate that tty connection.

# kill 

For the above example

# kill 331857

If the process doesn’t gracefully terminate, just as a last option you can forcefully kill by sending a SIGKILL

# kill -9 

Another way; single command to kill tty connections

You can also use the PKILL command along with the switch “-t” to kill a tty connection forcefully. Please see the commend pasted below:

# pkill -9 -t 

Example

# pkill -9 -t pts/0

 

How to check the current tty/pts session where you connected?

Yup, before going with the kill command, you must have an idea about your tty/pts session. This can be simply checked using the command ps or tty. See the usages pasted below:

Using ps

[root@connect ~]# ps
  PID TTY          TIME CMD
29849 pts/0    00:00:00 bash
29996 pts/0    00:00:00 ps

Using tty

[root@connect ~]# tty
/dev/pts/0

tty is the best command!!

That’s it!! Go ahead and kill _/\_ Thanks!

Hooooray…. It’s time to relax!! Just watch A Breakfast Ride To Chota Ladakh

Also read:

  1. Setup/configure a three node Elasticsearch cluster on CentOS 7
  2. A quick jump into AWS EC2 features – overview

Post navigation

Arunlal A

Senior System Developer at Zeta. Linux lover. Traveller. Let's connect! Whether you're a seasoned DevOps pro or just starting your journey, I'm always eager to engage with like-minded individuals. Follow my blog for regular updates, connect on social media, and let's embark on this DevOps adventure together! Happy coding and deploying!

8 thoughts on “How to kill or terminate unwanted tty/pts sessions in Linux?

  1. I have been through the whole content of this blog which is very informative and knowledgeable stuff, I tried a lot to run command for kill or terminate unwanted tty/pts sessions in Linux but I failed. This post helps me.

  2. Your system load average is over 1.0 and you haven’t rebooted in 234 days…what ARE you doing to that poor system? Not rebooting means you probably haven’t applied kernel updates, so there are probably some exploitable security vulnerabilities on that instance. Maxing out your uptime between reboots isn’t nearly as critical as applying all upstream kernel and security patches in a timely fashion. And the load average should be hanging around 0.1 even on a fairly busy system, which means you’ve got some CPU heavy code running on that system that desperately wants to be optimized (e.g. adding an index to a database table to deal with an expensive SQL query). A 2.0 load average is still somewhat usable but the system is hurting to the point of being noticeable by users and SSH is gonna start lagging like crazy. I keep all of my systems running smooth at a 0.1 (or less) load average and fully patched and I don’t allow massive resource hogs like WordPress to run on them.

    1. Manny, can you explain to me the statement of system load average? i think you need to correct your views about it.
      below a healthy system 🙂
      top – 23:11:24 up 4 days, 20:50, 1 user, load average: 13.48, 13.62, 13.90
      Tasks: 503 total, 1 running, 502 sleeping, 0 stopped, 0 zombie

      1. 1.0 is 100% CPU Usage on average.

        13.48 means that – On average – Your CPU is backlogged 1348%

        That is not a good thing. If your CPU is running at 100% usage 100% of the time, you probably have a rogue bitcoin miner you’re unaware about.

        1. 100% CPU usage is represented as 1.0 on a box that has 1 CPU. It is not uncommon to have the load average above 1.0 on a box running multiple CPUs. For example, a full load on a server with 12 cores and 2 CPUs per core would be 24.0.

          1. And Gary is correct. The load always have to be checked together with the amount of cores.
            I would change the wording a bit and say 2 sockets and 12 Cores per socket are 24 Cores 🙂

            Also Manny Boros is correct – at least with the uptime 😉

            I would prevent offering hostings in that environment.

  3. Good morning

    How can I obtain TTY terminal/information on a cellular phone.
    A deaf patient is harassing me daily with a TTY landline phone. The information is not showing on my cellular carriers phone logs.

    Please advised how to locate this TTY.

    Thanks
    Jennifer

  4. Why kill the child and not the parent? On my side it worked only when I killed the parent process!

Leave a Reply

Your email address will not be published. Required fields are marked *