如何判断我是否在屏幕中?

发布于 2024-10-26 08:45:14 字数 168 浏览 6 评论 0原文

在linux中使用screen时,如何判断自己是否在screen中? 我可以执行exit,如果我在一个屏幕中,我会退出一个屏幕,但如果我没有,那么我最终会关闭我的终端。

在执行 screen -r 时,我可以查看是否附加了其他屏幕,但我如何知道我当前的终端是否是这些附加屏幕之一?

When using screen in linux, how can I tell if I'm in a screen or not?
I could do exit and I'll exit a screen if I was in one, but if I wasn't, then I'll end up closing my terminal.

When doing screen -r, I could see if I have other screens attached, but how do I know if my current terminal is one of those attached screens?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

从﹋此江山别 2024-11-02 08:45:14

检查$STY。如果它为空,则您位于“真实”终端上。如果它包含任何内容,则它是您所在屏幕的名称。

如果您不在屏幕中:

eric@dev ~ $ echo $STY
eric@dev ~ $ 

如果您在屏幕中:

eric@dev ~ $ echo $STY
2026.pts-0.ip-10-0-1-71

Check $STY. If it's null, you're on a "real" terminal. If it contains anything, it's the name of the screen you're in.

If you are not in screen:

eric@dev ~ $ echo $STY
eric@dev ~ $ 

If you are in screen:

eric@dev ~ $ echo $STY
2026.pts-0.ip-10-0-1-71
晒暮凉 2024-11-02 08:45:14

我这样做的另一种方法是回显 $TERM。

 $ echo $TERM
 screen

由于我最终做了很多这样的事情,所以我在 .bashrc 文件中添加了一个别名:

alias trm='echo $TERM'

这样,无论是否在屏幕中,如果我只是执行“trm”,它会显示我是在 SCREEN 还是其他地方(通常是 XTERM) )。

Another way I've done it is to echo $TERM.

 $ echo $TERM
 screen

Since I end up doing this a lot, I added an alias into my .bashrc file:

alias trm='echo $TERM'

This way, whether in screen or not, if I just execute 'trm' it will show me whether I'm in SCREEN or elsewhere (usually XTERM).

明月夜 2024-11-02 08:45:14

检查您是否在屏幕中的另一种方法。

类型:

Ctrl-a ?

如果您看到屏幕帮助,则表明您处于屏幕中。

否则你会得到一个问号“?”在提示符上。

Alternative approach to check if you are in screen.

type:

Ctrl-a ?

If you see the screen help you are in screen.

Otherwise you'll get a question mark '?' on the prompt.

画▽骨i 2024-11-02 08:45:14

由于这里的所有其他方法都依赖于环境变量(可以简单地被覆盖)或屏幕的命令字符(也可以被覆盖),因此最简单的检查方法是列出当前进程的所有祖先。

pstree --show-parents -p $ | head -n 1 | sed 's/\(.*\)+.*/\1/' | grep screen | wc -l

如果它打印 1,那么您正在运行的当前进程的祖先进程的可执行文件名称中包含“screen”一词,否则就没有。

更容易的可见检查可以通过以下方式获得:

pstree --show-parents -p $ | head -n 1 | sed 's/\(.*\)+.*/\1/' | less

Since all of the other methods here rely on environment variables (which can simply be overridden) or the command character for screen (which can also be overridden), the most foolproof way to check would be to list all the ancestors of the current process.

pstree --show-parents -p $ | head -n 1 | sed 's/\(.*\)+.*/\1/' | grep screen | wc -l

If it prints 1, then the current process you're running has an ancestor with the word 'screen' in the executable's name, otherwise there wasn't.

A more facile visible inspection might be obtained from:

pstree --show-parents -p $ | head -n 1 | sed 's/\(.*\)+.*/\1/' | less
听你说爱我 2024-11-02 08:45:14

我对这个问题的解决方案要简单得多:只要点击TAB就会使整个终端闪烁(快速视频反转),如果您 >内部 GNU 屏幕

经过测试可在大多数 Linux(Ubuntu、Kali、Debian、RaspBerry...等)和 FreeBSD、GUI 以及任何终端(本地或远程)上运行,包括 Ctrl< /kbd>AltFn 个。

作为此方法的一个例外,请注意以下(相当复杂,但可能的)案例场景:

  • 1.- SSH 连接到计算机 A(假设是 Linux)。
  • 2.- 从计算机 A 上的远程终端输入新的 screen -S AScr
  • 3.- 从 GNU Screen AScr 终端通过 SSH 连接到计算机 B。
  • 4.- 输入新的屏幕来自计算机 B 上的远程终端的 screen -S BScr

在情况 2 和 4 中,您位于 Screen 内部,而在情况 1 中,您位于 Screen 外部和 3,但终端在情况 2、3 和 4 时会闪烁。

My solution to the problem is a lot simpler: just hitting TAB makes the full terminal blink (a quick video inversion) if you are inside GNU Screen.

Tested working on most Linux (Ubuntu, Kali, Debian, RaspBerry... etc) and FreeBSD, GUI and any terminal, local or remote, including CtrlAltFn ones.

As an exception for this method, please, note this (rather complex, but possible) case scenario:

  • 1.- SSH into computer A (lets assume Linux).
  • 2.- Enter a new screen -S AScr from remote terminal on computer A.
  • 3.- SSH from GNU Screen AScr terminal into Computer B.
  • 4.- Enter a new screen -S BScr from remote terminal on computer B.

You are inside a Screen on cases 2 and 4, and outside a Screen on cases 1 and 3, but the terminal will blink on cases 2, 3 and 4.

梦幻之岛 2024-11-02 08:45:14

当 ssh 进入远程(较旧)系统时,我注意到 $TERM 表明我正在使用“screen-256color”,但是没有 termcap/terminfo 条目,因此我被迫在 .bashrc 中求助于以下内容防止终端偶尔产生垃圾:

case $TERM in 
    (screen-256color) export TERM='screen'
esac

让它改用普通条目。

TL;DR,$TERM通常会指示远程 ssh 时您是否处于屏幕会话中。您可以使用 case $TERM in (screen*) echo "you are in a screen session"; esac 如果你只是想要一个视觉线索而不需要做一些具体的事情

While ssh'd into a remote (older) system I noticed that $TERM indicated I was using 'screen-256color', however there was no termcap/terminfo entry for that, so I was forced to resort to the following in .bashrc to prevent the terminal from producing occasional garbage:

case $TERM in 
    (screen-256color) export TERM='screen'
esac

to get it to use the plain entry instead.

TL;DR, $TERM will usually indicate if you are in a screen session when ssh'd remotely. You can use case $TERM in (screen*) echo "you are in a screen session"; esac if you just want a visual clue and don't need to do something specific

内心旳酸楚 2024-11-02 08:45:14

将以下一项或多项添加到 .bashrc

  • alias mysession='echo ${STY}'
  • alias myterm='echo ${TERM}'
  • <代码>alias isscreen='if test -n "$STY";然后 echo " 屏幕会话: ${STY}";否则 echo“不是屏幕会话”; fi'

然后您可以通过输入简单的命令知道您是否在屏幕内。

Add one or more of the followings into your .bashrc

  • alias mysession='echo ${STY}'
  • alias myterm='echo ${TERM}'
  • alias isscreen='if test -n "$STY"; then echo " screen session: ${STY}"; else echo " NOT a screen session"; fi'

Then you can know if you are inside a screen by typing simple commands.

远昼 2024-11-02 08:45:14

上述大多数答案的问题是,我们可能位于附加屏幕会话的子shell中。或者我们可能会从屏幕会话中打开远程主机的 shell。在前一种情况下,我们可以遍历进程树起源并匹配屏幕程序名称。在后一种情况下,大多数时候,我们可以检查 TERM 变量中是否有诸如 screen* 之类的内容。

我的答案类似于 /u/Parthian-Shot 但不太依赖于 pstree 实用程序;他使用的选项对我来说不可用。另一方面,我的实现仍然依赖于 Linux:对于非 Linux 系统,必须调整 ps 命令;对于具有不支持阵列的旧 shell 的系统,您将有更多的解决方法。但无论如何:

ps_walk_parents() {
  local tmp
  local ppid=$PPID
  while [[ $ppid != 1 ]]; do
    tmp=($( ps -o ppid,comm -p $ppid ))
    ppid=${tmp[0]}  # grab parent pid
    echo ${tmp[1]}  # output corresponding command name
  done
}
if [[ "$TERM" =~ screen* ]] || ps_walk_parents |grep -qxi screen ; then
  # we are in a screen terminal 
fi

我们可以稍微优化我们的函数,以停止搜索是否/当进程父进程与目标命令名称(“屏幕”)匹配时,但一般来说,该函数只会进行 2 到 3 次迭代。大概您想将此代码放入某些启动初始化中,例如 .bashrc 或 .profile 等,因此同样不值得优化。

The problem with most of the above answers is that we might be in a subshell of an attached screen session. Or we might be opening a shell to a remote host from within a screen session. In the former case, we can walk the process tree parentage and match for the screen program name. In the latter case, most of the time, we can check the TERM variable for something like screen*.

My answer os similar to /u/Parthian-Shot but not so dependent on the pstree utility; the options he use are not available to me. On the other hand, my implementation is still Linux-dependent: for non-Linux systems, one must tweak the ps command; for systems with older shells that don't support arrays, you'll have yet more work-arounds. But anyway:

ps_walk_parents() {
  local tmp
  local ppid=$PPID
  while [[ $ppid != 1 ]]; do
    tmp=($( ps -o ppid,comm -p $ppid ))
    ppid=${tmp[0]}  # grab parent pid
    echo ${tmp[1]}  # output corresponding command name
  done
}
if [[ "$TERM" =~ screen* ]] || ps_walk_parents |grep -qxi screen ; then
  # we are in a screen terminal 
fi

We could optimize our function a bit to stop searching if/when a process parent matches the target command name ("screen"), but in general, the function will only hit 2 to 3 iterations. Presumably you want to put this code in some startup initialization such as .bashrc or .profile or something, so again, not worth optimizing.

感受沵的脚步 2024-11-02 08:45:14

screen -ls 可以告诉你。

外屏:

$ screen -ls
There are screens on:
        16954.pts-1.auds916     (Detached)
        242.pts-8.auds916       (Detached)
2 Sockets in /tmp/screens/S-glennj.

内屏:

$ screen -ls
There are screens on:
        16954.pts-1.auds916     (Attached)
        242.pts-8.auds916       (Detached)
2 Sockets in /tmp/screens/S-glennj.

screen -ls can tell you.

Outside screen:

$ screen -ls
There are screens on:
        16954.pts-1.auds916     (Detached)
        242.pts-8.auds916       (Detached)
2 Sockets in /tmp/screens/S-glennj.

Inside a screen:

$ screen -ls
There are screens on:
        16954.pts-1.auds916     (Attached)
        242.pts-8.auds916       (Detached)
2 Sockets in /tmp/screens/S-glennj.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文