Linux 如何判断我是否在屏幕上?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5392618/
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
How can I tell whether I'm in a screen?
提问by user8125
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.
在 linux 中使用 screen 时,如何判断我是否在 screen 中?我可以exit
,如果我在一个屏幕上,我会退出一个屏幕,但如果我不在,那么我最终会关闭我的终端。
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?
这样做时screen -r
,我可以看到我是否连接了其他屏幕,但是我如何知道我当前的终端是否是这些附加屏幕之一?
采纳答案by jho
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.
检查$STY
。如果它为空,则您在“真实”终端上。如果它包含任何内容,则是您所在屏幕的名称。
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
回答by glenn Hymanman
screen -ls
can tell you.
screen -ls
可以告诉你。
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.
回答by JJC
Another way I've done it is to echo $TERM.
我完成的另一种方法是回显 $TERM。
$ echo $TERM
screen
Since I end up doing this a lot, I added an alias into my .bashrc file:
由于我最终这样做了很多,我在我的 .bashrc 文件中添加了一个别名:
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).
这样,无论是否在屏幕中,如果我只是执行“trm”,它就会显示我是在屏幕中还是在其他地方(通常是 XTERM)。
回答by pors
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.
否则你会得到一个问号“?” 在提示上。
回答by Parthian Shot
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.
由于这里的所有其他方法都依赖于环境变量(可以简单地覆盖)或 screen 的命令字符(也可以覆盖),因此最简单的检查方法是列出当前进程的所有祖先。
pstree --show-parents -p $$ | head -n 1 | sed 's/\(.*\)+.*//' | 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.
如果它打印 1,那么您正在运行的当前进程在可执行文件的名称中有一个带有“屏幕”一词的祖先,否则没有。
A more facile visible inspection might be obtained from:
可以从以下位置获得更容易的可见检查:
pstree --show-parents -p $$ | head -n 1 | sed 's/\(.*\)+.*//' | less
回答by WebDragon
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:
当 ssh 进入远程(旧)系统时,我注意到 $TERM 表示我正在使用“screen-256color”,但是没有 termcap/terminfo 条目,所以我不得不求助于 .bashrc 中的以下内容防止终端偶尔产生垃圾:
case $TERM in
(screen-256color) export TERM='screen'
esac
to get it to use the plain entry instead.
让它改用普通条目。
TL;DR, $TERM will usuallyindicate 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
TL;DR, $TERM通常会在远程 ssh 时指示您是否在屏幕会话中。case $TERM in (screen*) echo "you are in a screen session"; esac
如果您只想要一个视觉线索而不需要做一些特定的事情,您可以使用
回答by Lee
Add one or more of the followings into your .bashrc
将以下一项或多项添加到您的 .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'
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.
然后你可以screen
通过输入简单的命令来知道你是否在 a 中。
回答by Otheus
The problem with most of the above answers is that we might be in a subshellof 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*
.
上述大多数答案的问题在于,我们可能处于附加屏幕会话的子外壳中。或者我们可能正在从屏幕会话中打开一个到远程主机的 shell。在前一种情况下,我们可以遍历进程树的父系并匹配screen
程序名称。在后一种情况下,大多数情况下,我们可以检查TERM
变量中的类似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:
我的答案 os 类似于 /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
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”)时停止搜索,但通常,该函数只会命中 2 到 3 次迭代。大概你想把这段代码放在一些启动初始化中,比如 .bashrc 或 .profile 之类的,所以再说一遍,不值得优化。
回答by Sopalajo de Arrierez
My solution to the problem is a lot simpler: just hittingTABmakes the full terminal blink(a quick video inversion) if you are insideGNU Screen.
我对这个问题的解决方案要简单得多:如果您在GNU Screen 中,只需点击即可TAB使整个终端闪烁(快速视频反转)。
Tested working on most Linux(Ubuntu, Kali, Debian, RaspBerry... etc) and FreeBSD, GUI and any terminal, local or remote, including CtrlAltFnones.
在大多数Linux(Ubuntu、Kali、Debian、RaspBerry 等)和FreeBSD、GUI 和任何本地或远程终端(包括终端)CtrlAltFn上进行了测试。
As an exceptionfor 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.
- 1.- SSH 到计算机 A(假设 Linux)。
- 2.-
screen -S AScr
在计算机 A 上从远程终端输入一个新的。 - 3.- 从 GNU Screen
AScr
终端通过SSH 连接到计算机 B。 - 4.-
screen -S BScr
在计算机 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.
您Screen
在情况 2 和 4 中处于 a 内部,Screen
在情况 1 和 3 中处于 a 外部,但终端将在情况 2、3 和 4 中闪烁。