在 bash 脚本中使用屏幕

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20880883/
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-09-18 09:07:56  来源:igfitidea点击:

Using screen in bash script

linuxbashgnu-screen

提问by AreusAstarte

I'm running a game server on a remote server where I use a detached screen instance to leave it running. I'm now creating a script that can be used to shut down the server, back up all the vital files and start it up again, however I'm having a few difficulties with dealing with the screen.

我在远程服务器上运行游戏服务器,在那里我使用分离的屏幕实例让它继续运行。我现在正在创建一个脚本,可用于关闭服务器、备份所有重要文件并重新启动它,但是我在处理屏幕时遇到了一些困难。

I assumed that I could just switch into the detached screen in the script (after the server had already been shut down) by calling screen -r in the script. But that doesn't seem to work because if I run the script from outside screen it just launches the server in that session.

我假设我可以通过在脚本中调用 screen -r 来切换到脚本中的分离屏幕(在服务器已经关闭之后)。但这似乎不起作用,因为如果我从外部屏幕运行脚本,它只会在该会话中启动服务器。

screen -r
cd ~/servers/StarMade/
sh StarMade-dedicated-server-linux.sh
screen -d

This is what I thought would do the trick but it doesn't. Maybe somebody can help me out here. I'm not a bash expert. In fact this is propably my first bash script that doesn't include "Hello World". Thanks.

这就是我认为可以解决问题的方法,但事实并非如此。也许有人可以在这里帮助我。我不是 bash 专家。事实上,这可能是我的第一个不包含“Hello World”的 bash 脚本。谢谢。

回答by Guntram Blohm supports Monica

Your script, as in your example, will get executed by your sell, not the one in the screen. You need to tell the running screen to read a file and execute it - that's what the -X option is for.

在您的示例中,您的脚本将由您的销售执行,而不是屏幕中的脚本。您需要告诉正在运行的屏幕读取文件并执行它 - 这就是 -X 选项的用途。

Try

尝试

tempfile=$(mktemp)
cat > $tempfile <<EOF
cd ~/servers/StarMade/
sh StarMade-dedicated-server-linux.sh
EOF
screen -X readbuf $tempfile
screen -X paste .
rm -f $tempfile

You can leave screen running in a 2nd terminal session to see what happens.

您可以让屏幕在第二个终端会话中运行以查看会发生什么。