For using IRC, I usually open irssi on my headless server in a screen session. Then, sometimes I detach it to wait for an answer, but sometimes I'm not sure if a screen with irssi is already open on a server.

For these times, I'd like to be able to tell screen: "Reattach to the running screen, but if it doesn't exist, create a new one by running this command". I tried to do this with:

$ screen -R irssi

but it doesn't show the desired behavior (instead, it opens an empty screen session when none exists, without running irssi).


Not sure if that covers all your requirements

screen -dRq sessionname irssi

If there's no screen running, it creates a new session with name "sessionname" and starts irssi. The "q" suppresses the new screen confirmation.

When you are detached, it reattaches to the existing "sessionname" that runs irssi.

Not sure if it works with multiple screen sessions, though, i.e. I simply haven't tried it

  • This does everything I need, but it seems that in your call, "bash" isn't part of the command but the name of the screen being created. That would also explain why my first try didn't work (I specified just a name and no command). Also, I don't understand how this works ;) Especially the combination of -d -R -m.
    – user208360
    Mar 18 '13 at 21:15
  • If I use the command above, the window in the window list is called "irssi" not "bash". I have to admit, I'm not a screen guru ;) I usually use screen -dR to start a screen session. The -q came from the man page. And the -m bash irssi came from a web search. I just added it all together and it worked. But I can't really explain the last bit either ;-) Mar 18 '13 at 21:42

If irssi is the thing you are almost always running in screen, then why not setup a a .screenrc that automatically starts irssi. Then just run screen -R. If your session isn't running, then it will start.

# more .screenrc 
startup_message off
term linux
screen 0  irssi

If you don't want your screen to start irssi by default you could simply create an rc file with a non-default name. Perhaps .screenrc-irssi. Then point to a particular config file like so screen -c .screenrc-irssi.

  • Thank you, doing this and issuing 'screen -c .screen-irssi -R' does what I want. Now, the only thing that's a little annoying is the "New screen..." message that appears when -R creates a new screen, which I have to confirm with a Return or Space. Is there any way to turn that off?
    – user208360
    Mar 18 '13 at 19:58
  • @mtu, Kinda looks like it requires re-compiling. See: superuser.com/questions/182477/…
    – Zoredache
    Mar 18 '13 at 20:32

Hmm, I am slightly confused by the existing answers. But if running two invocations of screen is not a problem you can always do:

screen -xS sessionname || screen -S sessionname irrsi

This will attempt to attach to the existing one without detaching any other screen and, failing that, create a new session with that name. This is what I have aliased in my .bashrc and it works like a charm.

But I am migrating to a new system and with it to tmux. You might want to give that a try, too. Way more convenient configuration file syntax, free-form splits and lots of options and commands that can be bound to shortcuts that suit you best.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy