Linux 检查指定名称的屏幕是否存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12255388/
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
Checking if a Screen of the Specified Name Exists
提问by Zoey
I have made a bash file which launches another bash file in a detached screen with a unique name, I need to ensure that only one instance of that internal bash file is running at any one point in time. To do this, I want to make the parent bash file check to see if the screen by that name exists before attempting to create it. Is there a method to do this?
我制作了一个 bash 文件,它在分离的屏幕中以唯一的名称启动另一个 bash 文件,我需要确保在任何一个时间点只有一个该内部 bash 文件的实例正在运行。为此,我想让父 bash 文件在尝试创建之前检查该名称的屏幕是否存在。有没有办法做到这一点?
采纳答案by chepner
You can grep the output of screen -list
for the name of the session you are checking for:
您可以 grep 输出screen -list
您正在检查的会话的名称:
if ! screen -list | grep -q "myscreen"; then
# run bash script
fi
回答by troyfolger
You can query the screen 'select' command for a particular session; the shell result is '0' if the session exists, and '1' if the named screen session is not found:
您可以查询特定会话的屏幕“选择”命令;如果会话存在,shell 结果为“0”,如果未找到指定的屏幕会话,则为“1”:
$ screen -S Tomcat $ screen -S Tomcat -Q select . ; echo $? 0
versus:
相对:
$ screen -S Jetty -Q select . ; echo $? No screen session found. 1
Note that the '.'
after the select
is optional, but may be more robust.
请注意,'.'
afterselect
是可选的,但可能更健壮。
回答by staircase27
Given I can't comment I am posting this as a new answer. troyfolger's answer is a good idea and basically amounts to try and send the session a command that will do very little. One issue is that for some (older) versions of screen -Q isn't supported and so for those versions the correct command is
鉴于我无法发表评论,我将此作为新答案发布。troyfolger 的回答是一个好主意,基本上相当于尝试向会话发送一个几乎没有作用的命令。一个问题是,不支持某些(旧)版本的 screen -Q,因此对于这些版本,正确的命令是
screen -S Jetty -X select . ; echo $?
Which send the command "select ." to the screen session called "Jetty".
其中发送命令“select”。到名为“Jetty”的屏幕会话。
Select changes which window is active and . means the current active window so this means try and change the active window to the currently active window. This can only fail if there isn't a session to connect to which is what we wanted.
选择更改哪个窗口处于活动状态和 。表示当前活动窗口,因此这意味着尝试将活动窗口更改为当前活动窗口。只有当没有我们想要的会话连接时,这才会失败。
If you read the info docs than it does suggest that the only use of select . is with -X as a test or to make sure something is selected.
如果您阅读了信息文档,那么它确实建议您只使用 select 。使用 -X 作为测试或确保选择了某些内容。
回答by KrisWebDev
All the solutions proposed don't deal with screen names that don't have unique patterns, e.g. "TEST" and "TEST123". When you screen -S "TEST"
or screen -list "TEST"
, you may find yourself selecting the screen "TEST123"! There is something wrong (non-deterministic) in how GNU screen implements screen name matching.
提出的所有解决方案都不会处理没有唯一模式的屏幕名称,例如“TEST”和“TEST123”。当您screen -S "TEST"
或 时screen -list "TEST"
,您可能会发现自己选择了屏幕“TEST123”!GNU screen 实现屏幕名称匹配的方式有问题(非确定性)。
Below is a bash function that tries to do exact matches, and return the PID.SCREEN NAME
along with an exit code:
下面是一个 bash 函数,它尝试进行精确匹配,并返回PID.SCREEN NAME
和退出代码:
function find_screen {
if screen -ls "" | grep -o "^\s*[0-9]*\.[ "$'\t'"](" --color=NEVER -m 1 | grep -oh "[0-9]*\." --color=NEVER -m 1 -q >/dev/null; then
screen -ls "" | grep -o "^\s*[0-9]*\.[ "$'\t'"](" --color=NEVER -m 1 | grep -oh "[0-9]*\." --color=NEVER -m 1 2>/dev/null
return 0
else
echo ""
return 1
fi
}
Usage - select a screen:
用法 - 选择一个屏幕:
target_screen=$(find_screen "SCREEN NAME")
screen -S "$target_screen" ...etc...
Usage - test if a screen exists:
用法 - 测试屏幕是否存在:
if find_screen "SCREEN NAME" >/dev/null; then
echo "Found!"
fi
Anyway, this will cover 99,9% cases. To be 99,99% sure, escape grep special chars in the screen name. A perfect match would require grep to match the whole line until $, including the date in parenthesis that may evolve with versions. The other perfect match method would be:
无论如何,这将涵盖 99.9% 的情况。为确保 99,99% 的把握,请转义屏幕名称中的 grep 特殊字符。完美匹配需要 grep 匹配整行直到 $,包括括号中的日期,这些日期可能随版本而变化。另一种完美匹配方法是:
ls -A -1 /var/run/screen/S-${USER} | grep "^[0-9]*\.SCREEN NAME$"
But that's hacky, and we need to be sure that screen implementation uses this folder. I don't recommend this last method.
但这很麻烦,我们需要确保屏幕实现使用此文件夹。我不推荐最后一种方法。
回答by An?l Eroglu
%100 Work.
%100 工作。
screen -list | grep "SESS?ON NAME" && echo "Active Program" || echo "Passive Program"
回答by Badr Elmers
a simpler method is :
一个更简单的方法是:
screen -xR -S SessionName
copied from a Stéphane Chazelascomment from here:
复制自这里的Stéphane Chazelas评论:
I generally use?-xR?to attach or create if there's nothing to attach.
如果没有要附加的内容,我通常使用?-xR? 来附加或创建。
so like that you do not have to search if the session name already exist, this method will attach to the session if it exist and if not then it will creat it.
这样您就不必搜索会话名称是否已经存在,如果会话存在,则此方法将附加到会话,如果不存在,则将创建它。