Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top