Linux 如何在 unix 中杀死 tty

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15195470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:11:06  来源:igfitidea点击:

how to kill the tty in unix

linuxunix

提问by Vidya Sagar

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

这是finger命令的结果(我(Vidya)登录时的今天(星期一))

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/5are the current active terminals.

在列下Tty pts/0 and pts/5是当前活动的终端。

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

除了这两个pts/6pts/9并且pts/10也在场,我上周登录了这些。但是它们的空闲时间显示为“-”(非空闲)。

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

我怎样才能杀死这些 6,9 和 10 终端?

回答by Tuxdude

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 PIDfrom the result.

抓住PID从结果。

Use the PIDs to kill the processes:

使用 PID 来终止进程:

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

如果进程没有正常终止,作为最后一个选项,您可以通过发送 SIGKILL

kill -9 <PID>

回答by AIXroot

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".

我今天遇到了同样的问题。我没有剩余的进程,但是用户“xxx”的剩余手指条目阻止了我使用“userdel xxx”删除该用户。

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

错误消息是:userdel:当前正在使用帐户“xxx”。

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:

所以我(重新)移动了 /var/run/utmp 文件:

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.

这清除了所有手指条目。不幸的是,通过这种方式,即使当前正在运行的会话也将被清除。如果这对您来说是个问题,您必须在(重新)移动 utmp 文件后重新启动。

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

但是,在我的情况下,这是解决方案。之后我能够使用“userdel xxx”成功删除用户。

回答by Jacob Bryan

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.

我和你有同样的问题,但我想杀死我所在的 gnome 终端。我阅读了关于“who”的手册,发现你可以使用“-a”选项列出登录到你的计算机的所有会话,并且然后'-l'选项打印系统登录进程。

who -la

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

谁给了我什么你应该得到这样的东西。然后你所要做的就是使用“kill”命令终止进程。

kill <PID>

回答by visionedison

Try this:

尝试这个:

skill -KILL -v pts/6

skill -KILL -v pts/9

skill -KILL -v pts/10

回答by samtoddler

You can use killall command as well .

您也可以使用 killall 命令。

-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,

-o, --older-than 只匹配比指定时间更早(在之前开始)的进程。时间被指定为一个浮点数,然后是一个单位。单位为 s,m,h,d,w,M,y 表示秒、分、小时、天、

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

-e, --exact 要求非常长的名称完全匹配。

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

-r, --regexp 将进程名称模式解释为扩展的正则表达式。

This worked like a charm.

这就像一个魅力。

回答by Dragos Alexe

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

如果要在所有过程中为特定用户关闭 tty,上面的命令是最简单的。您可以使用:

killall -u user_name

回答by T-Bee

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

您不需要知道点数,只需键入:

ps all | grep bash

then:

kill pid1 pid2 pid3 ...

ps 全部 | grep bash

然后:

杀死pid1 pid2 pid3 ...

回答by dli

for example kill pts/0

例如杀死 pts/0

pkill -9 -t pts/0

回答by Twirlip

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:

除了AIXroot的回答,还有一个注销功能,可以用来写utmp注销记录。所以如果你没有用户xxxx的任何进程,但是userdel说“userdel: account xxxx is current in use”,你可以手动添加一个注销记录。创建一个文件 logout.c 像这样:

#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的“On since”行的输出中运行:

# 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

回答by Maxwell de Assis

The simplest way is with the pkillcommand. In your case:

最简单的方法是使用pkill命令。在你的情况下:

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

Regarding tty sessions, the commands below are always useful:

关于 tty 会话,下面的命令总是有用的:

w- shows active terminal sessions

w- 显示活动终端会话

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

tty- 显示您当前的终端会话(因此您不会意外关闭它)

last | grep logged- shows currently logged users

last | grep logged- 显示当前登录的用户

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.

pkill -u username- 杀死“用户名”用户的所有会话。

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