Checking if a Screen of the Specified Name Exists

Zoey picture Zoey · Sep 4, 2012 · Viewed 28.5k times · Source

I have made a bash file which launches another bash file in a detached screen with a unique name, I need to ensure that only one instance of that internal bash file is running at any one point in time. To do this, I want to make the parent bash file check to see if the screen by that name exists before attempting to create it. Is there a method to do this?

Answer

chepner picture chepner · Sep 4, 2012

You can grep the output of screen -list for the name of the session you are checking for:

if ! screen -list | grep -q "myscreen"; then
    # run bash script
fi