sh screen - Wait for screen to terminate

本秂侑毒 提交于 2019-12-01 05:04:22

问题


I'm writing on a little script that does send a command to a running screen session. This command stops the screen but not instantly. I need to wait for it to finish in order to continue with the rest of the script.

This is how I stop the screen:

screen -S $SCREEN_NAME -p 0 -X stuff "`printf "stop\r"`"

How could I do this?


回答1:


I found an solution:

You just simply check if the screen is still running and wait (sleep) for one second.

Like this:

while screen -list | grep -q $SCREEN_NAME
do
    sleep 1
done



回答2:


For *nix systems, you can use the wait command in your shell script. wait can take a PID as well, so you can do wait $! to wait on the last executed command run in background. If your commands are not running in background (no & at the end of the command), then running your script will wait until that command finishes




回答3:


If you reattach the screen session, then it will automatically wait until all the processes within are done. And if you try to reattach to a session that is already done, it will just error out immediately.

If you want to run in "headless" mode, then BrainStone's answer is good enough. Just be sure to name your session ($SCREEN_NAME) uniquely. Something like SCREEN_NAME=$(date +my_screen_thing_+%H%M%S) to get a timestamp on it.



来源:https://stackoverflow.com/questions/18087964/sh-screen-wait-for-screen-to-terminate

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