Using screen in bash script

て烟熏妆下的殇ゞ 提交于 2020-01-13 12:52:22

问题


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
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.


回答1:


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.

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.



来源:https://stackoverflow.com/questions/20880883/using-screen-in-bash-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!