You can already do this with existing screen
options without explicitly checking for a named session. screen -D -R serverman
will attach to serverman
if exists, but will create it and attach to it if not.
Question
Basically i need to have a script that checks if there is a screen running as "serverman". If it exists, then resume it, otherwise create new.
So i wanted to parse "sceen -ls" and check if there is one called "serverman".
This is "screen -ls" output
There are screens on:
7423.212 (03/09/14 08:48:58) (Detached)
7411.1as (03/09/14 08:48:49) (Detached)
2 Sockets in /var/run/screen/S-root.
I was tinking to a command that could parse it trough piping and search for "serverman" word. And if any set isalreadyrunning=1, else set it to 0, but i don't know what command to use and how to use, do you?
Solution
OTHER TIPS
I paste a working script for tmux
, as apparently I have old screen
on my old MacBook that gives different output. You should be able to modify it easily (but I would advice to switch to tmux
, it's way better)
# created sessions with
# tmux new -s hello
# tmux new -s world
SEARCHING="hello"
found_session=$(tmux ls 2> /dev/null | perl -ne "if (/(^${SEARCHING}):/) {print \"\$1\"}" | head -n1)
if [ -z "$found_session" ]; then
echo Session not found, creating new.
tmux new -s $SEARCHING
else
echo Found $found_session, attaching.
tmux a -t $found_session
fi
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow