如何判断我是否在屏幕中?
在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
检查
$STY
。如果它为空,则您位于“真实”终端上。如果它包含任何内容,则它是您所在屏幕的名称。如果您不在屏幕中:
如果您在屏幕中:
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:
If you are in screen:
我这样做的另一种方法是回显 $TERM。
由于我最终做了很多这样的事情,所以我在 .bashrc 文件中添加了一个别名:
这样,无论是否在屏幕中,如果我只是执行“trm”,它会显示我是在 SCREEN 还是其他地方(通常是 XTERM) )。
Another way I've done it is to echo $TERM.
Since I end up doing this a lot, I added an alias into my .bashrc file:
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).
检查您是否在屏幕中的另一种方法。
类型:
如果您看到屏幕帮助,则表明您处于屏幕中。
否则你会得到一个问号“?”在提示符上。
Alternative approach to check if you are in screen.
type:
If you see the screen help you are in screen.
Otherwise you'll get a question mark '?' on the prompt.
由于这里的所有其他方法都依赖于环境变量(可以简单地被覆盖)或屏幕的命令字符(也可以被覆盖),因此最简单的检查方法是列出当前进程的所有祖先。
如果它打印 1,那么您正在运行的当前进程的祖先进程的可执行文件名称中包含“screen”一词,否则就没有。
更容易的可见检查可以通过以下方式获得:
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.
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:
我对这个问题的解决方案要简单得多:只要点击TAB就会使整个终端闪烁(快速视频反转),如果您 >内部 GNU 屏幕。
经过测试可在大多数 Linux(Ubuntu、Kali、Debian、RaspBerry...等)和 FreeBSD、GUI 以及任何终端(本地或远程)上运行,包括 Ctrl< /kbd>AltFn 个。
作为此方法的一个例外,请注意以下(相当复杂,但可能的)案例场景:
screen -S AScr
。AScr
终端通过 SSH 连接到计算机 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:
screen -S AScr
from remote terminal on computer A.AScr
terminal into Computer B.screen -S BScr
from remote terminal on computer B.You are inside a
Screen
on cases 2 and 4, and outside aScreen
on cases 1 and 3, but the terminal will blink on cases 2, 3 and 4.当 ssh 进入远程(较旧)系统时,我注意到 $TERM 表明我正在使用“screen-256color”,但是没有 termcap/terminfo 条目,因此我被迫在 .bashrc 中求助于以下内容防止终端偶尔产生垃圾:
让它改用普通条目。
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:
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将以下一项或多项添加到
.bashrc
alias mysession='echo ${STY}'
alias myterm='echo ${TERM}'
然后您可以通过输入简单的命令知道您是否在
屏幕
内。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.上述大多数答案的问题是,我们可能位于附加屏幕会话的子shell中。或者我们可能会从屏幕会话中打开远程主机的 shell。在前一种情况下,我们可以遍历进程树起源并匹配屏幕程序名称。在后一种情况下,大多数时候,我们可以检查
TERM
变量中是否有诸如screen*
之类的内容。我的答案类似于 /u/Parthian-Shot 但不太依赖于 pstree 实用程序;他使用的选项对我来说不可用。另一方面,我的实现仍然依赖于 Linux:对于非 Linux 系统,必须调整 ps 命令;对于具有不支持阵列的旧 shell 的系统,您将有更多的解决方法。但无论如何:
我们可以稍微优化我们的函数,以停止搜索是否/当进程父进程与目标命令名称(“屏幕”)匹配时,但一般来说,该函数只会进行 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 theTERM
variable for something likescreen*
.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: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.
screen -ls
可以告诉你。外屏:
内屏:
screen -ls
can tell you.Outside screen:
Inside a screen: