[linux] how to kill the tty in unix

This is the result of the finger command (Today(Monday) when I (Vidya) logged in)

sekic1083 [6:14am] [/home/vidya] -> finger
Name        Tty       Idle   Login Time    Where
Felix   pts/0        -       Thu 10:06  sekic2594.rnd.ki.sw.
john        pts/1       2d       Fri 15:43
john        *pts/2      2d       Fri 15:43
john      *pts/3       4     Fri 15:44
john      *pts/7       -         Thu 16:25
Vidya      pts/0       -         Mon 06:14
Vidya     *pts/5       -         Mon 06:14
Vidya     *pts/6       -         Tue 10:13
Vidya     *pts/9       -         Wed 05:39
Vidya     *pts/10      -         Wed 10:23

Under column the Tty pts/0 and pts/5 are the current active terminals.

Apart from those two pts/6, pts/9 and pts/10 are also present and I had logged into these last week. But the idle time for them is showing as "-" (not idle).

How can I kill these 6,9 and 10 terminals?

This question is related to linux unix

The answer is


The simplest way is with the pkill command. In your case:

pkill -9 -t pts/6
pkill -9 -t pts/9
pkill -9 -t pts/10

Regarding tty sessions, the commands below are always useful:

w - shows active terminal sessions

tty - shows your current terminal session (so you won't close it by accident)

last | grep logged - shows currently logged users

Sometimes we want to close all sessions of an idle user (ie. when connections are lost abruptly).

pkill -u username - kills all sessions of 'username' user.

And sometimes when we want to kill all our own sessions except the current one, so I made a script for it. There are some cosmetics and some interactivity (to avoid accidental running on the script).

#!/bin/bash
MYUSER=`whoami`
MYSESSION=`tty | cut -d"/" -f3-`
OTHERSESSIONS=`w $MYUSER | grep "^$MYUSER" | grep -v "$MYSESSION" | cut -d" " -f2`
printf "\e[33mCurrent session\e[0m: $MYUSER[$MYSESSION]\n"

if [[ ! -z $OTHERSESSIONS ]]; then
  printf "\e[33mOther sessions:\e[0m\n"
  w $MYUSER | egrep "LOGIN@|^$MYUSER" | grep -v "$MYSESSION" | column -t
  echo ----------
  read -p "Do you want to force close all your other sessions? [Y]Yes/[N]No: " answer
  answer=`echo $answer | tr A-Z a-z`
  confirm=("y" "yes")

  if [[ "${confirm[@]}" =~ "$answer" ]]; then
  for SESSION in $OTHERSESSIONS
    do
         pkill -9 -t $SESSION
         echo Session $SESSION closed.
    done
  fi
else
        echo "There are no other sessions for the user '$MYUSER'".
fi

You can run:

ps -ft pts/6 -t pts/9 -t pts/10

This would produce an output similar to:

UID        PID  PPID  C STIME TTY          TIME CMD
Vidya      772  2701  0 15:26 pts/6    00:00:00 bash
Vidya      773  2701  0 16:26 pts/9    00:00:00 bash
Vidya      774  2701  0 17:26 pts/10   00:00:00 bash

Grab the PID from the result.

Use the PIDs to kill the processes:

kill <PID1> <PID2> <PID3> ...

For the above example:

kill 772 773 774

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

kill -9 <PID>

In addition to AIXroot's answer, there is also a logout function that can be used to write a utmp logout record. So if you don't have any processes for user xxxx, but userdel says "userdel: account xxxx is currently in use", you can add a logout record manually. Create a file logout.c like this:

#include <stdio.h>
#include <utmp.h>

int main(int argc, char *argv[])
{
  if (argc == 2) {
    return logout(argv[1]);
  }
  else {
    fprintf(stderr, "Usage: logout device\n");
    return 1;
  }
}

Compile it:

gcc -lutil -o logout logout.c

And then run it for whatever it says in the output of finger's "On since" line(s) as a parameter:

# finger xxxx
Login: xxxx                             Name:
Directory: /home/xxxx                   Shell: /bin/bash
On since Sun Feb 26 11:06 (GMT) on 127.0.0.1:6 (messages off) from 127.0.0.1
On since Fri Feb 24 16:53 (GMT) on pts/6, idle 3 days 17:16, from 127.0.0.1
Last login Mon Feb 10 14:45 (GMT) on pts/11 from somehost.example.com
Mail last read Sun Feb 27 08:44 2014 (GMT)
No Plan.

# userdel xxxx
userdel: account `xxxx' is currently in use.
# ./logout 127.0.0.1:6
# ./logout pts/6
# userdel xxxx
no crontab for xxxx

You can use killall command as well .

-o, --older-than Match only processes that are older (started before) the time specified. The time is specified as a float then a unit. The units are s,m,h,d,w,M,y for seconds, minutes, hours, days,

-e, --exact Require an exact match for very long names.

-r, --regexp Interpret process name pattern as an extended regular expression.

This worked like a charm.


I had the same question as you but I wanted to kill the gnome terminal which I was in. I read the manual on "who" and found that you can list all of the sessions logged into your computer with the '-a' option and then the '-l' option prints the system login processes.

who -la

What who gave me You should get something like this. Then all you have to do is kill the process with the 'kill' command.

kill <PID>

for example kill pts/0

pkill -9 -t pts/0

you do not need to know pts number, just type:

ps all | grep bash

then:

kill pid1 pid2 pid3 ...


If you want to close tty for specific user with all the process, above command is the easiest. You can use:

killall -u user_name

I had the same problem today. I had NO remaining processes, but the remaining finger entry of user "xxx", which prevent me the deletion of this user using "userdel xxx".

Error message was: userdel: account `xxx' is currently in use.

It looked like a crashed terminal session. So I rebooted, but the issue remained.

last xxx
xxx      pts/5        10.1.2.3     Fri Feb  7 10:25 - crash  (01:27)

So I (re)moved the /var/run/utmp file:

mv /var/run/utmp /var/run/utmp.save ; touch /var/run/utmp

This cleared all finger entries. Unfortunately in this way even the current running sessions will be cleared. If this is an issue for you, you have to reboot, after you (re)moved the utmp file.

However in my case this was the solution. Afterwards I was able to successfully delete the user, using "userdel xxx".


Try this:

skill -KILL -v pts/6

skill -KILL -v pts/9

skill -KILL -v pts/10