how to kill the tty in unix

前端 未结 10 1506
遇见更好的自我
遇见更好的自我 2020-12-23 20:52

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

sekic1083 [6:14am] [/home/vidya] -> finger
Name        Tty          


        
10条回答
  •  粉色の甜心
    2020-12-23 21:55

    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
    

提交回复
热议问题