Reputation: 3155
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?
Upvotes: 7
Views: 7319
Reputation: 20818
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.
Upvotes: 0
Reputation: 3155
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
Upvotes: 13
Reputation: 678
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
Upvotes: 0