Jacob Ilyane
Jacob Ilyane

Reputation: 93

How to attach screen seesion or create it?

I am currently using this to launch mutt or irssi:

urxvt -name Irssi/Mutt screen -r Irssi/Mutt

Currently I have to do the following before using my launcher:

screen -S Irssi/Mutt irssi/mutt + Ctrl-a-d

What I am looking to do is:

if [ test_to_see_if_the_screen_exit ]  # I need a way to the test
then
  urxvt -name Irssi/Mutt -e screen -r Irssi/Mutt
else
  create_the_screen_named_Irssi/Mutt_and_detach_it # I need a way to create it
  urxvt -name Irssi/Mutt -e screen -r Irssi/Mutt
endif

Does anyone have a solution?

Upvotes: 2

Views: 207

Answers (2)

twalberg
twalberg

Reputation: 62369

Use screen -list or screen -ls to show your existing screens.

I would probably do your if...endif bit this way, though:

screen_opts=""
case $(screen -list Irssi/Mutt | awk '/Irssi/{print $NF}') in
  *Attached*) ;; # not sure what you would want here,
                 # but I would probably do 'screen_opts="-x"'...
  *Detached*) screen_opts="-r" ;;
  *) screen -wipe # if session is dead, clean it up
     screen_opts="-S Irssi/Mutt";;
esac
urxvt -name Irssi/Mutt -e screen ${screen_opts}

Upvotes: 1

Sir Athos
Sir Athos

Reputation: 9867

You can use screen -list | grep Irssi/Mutt to see if your session already exists.

But it's easier to just let screen figure out if the session exists:

screen -r Irssi/Mutt || screen -S Irssi/Mutt irssi/mutt

This will try to attach to an existing session, and create a new one if attaching fails (and you don't need to detach and reattach right away, just stay in the session).

To make urxvt run that, you'll have to specify sh explicitly:

urxvt -name Irssi/Mutt -e sh -c 'screen -r Irssi/Mutt || screen -S Irssi/Mutt irssi/mutt'

Upvotes: 1

Related Questions