Command-Line Multitasking with Screen

By | 2007/05/04

Screen.  What can I say about it?  It’s not very well known to many people I mention it to but its one of those programs that you just can’t stop using once you’ve started.  Yes, I use screen on a regular daily basis and it’s really easy to use.

I know someone who says the only reason they invented X was so that you could use more virtual terminals.  I think the real solution behind great command line multitasking is screen!

Here are some basic usage tips.  To start a command line based program within a virtual ‘screen’ prefix that command with screen, such as:

screen irssi

Once that has loaded you can detach the virtual screen for later use by pressing ctrl-a d.  To reattach that screen type screen -r (or, if the screen was somehow not completely detached previously you would use screen -dr).

Now, ok, I can start applications within screen and detach them.. that’s cool I guess but couldn’t I just background the process or something similar?  I suppose so, but here is where it gets interesting.

Create a new screen instance within screen with ctrl-a c.  Do this as many times as you might need, based on the number of apps you’re using.  To later switch between these screens use ctrl-a 0-9, ctrl-a n,p (for next or previous) or to see a visual list, use ctrl-a “.

To close a screen you no longer need you can simply type exit which will take you to the next screen or exit screen completely if it is the last one.

In my setup I’m running irssi, mutt, bash and snownews within screen 0-3 over ssh to a private server at home.  On my local machine I load screen for rtorrent and an irssi notification script.  Once I get them both going I ctrl-a d, close gnome-terminal and don’t bother with those apps until I need something. At that point I screen -r and there they are again, right back where I left them.. and having continued that whole time.  Like I said, easy multitasking on the command line.

Do you have any magic screen tips to share with the crowd?  You know where the comments go.

24 thoughts on “Command-Line Multitasking with Screen

  1. vincent

    I really like the idea of screen, but every time I tried to use it, I gave up for a tiny and bad reason : ctrl-a.

    As a matter of fact, ctrl-A is a readline shortcut to place the cursor at the beginning of the current line. I use it a lot, and not being able to use it inside a screen session is very annoying.
    I didn’t find a quick trick to solve this problem, but if you had a solution I’d like to hear it :)

    Reply
    1. nibb

      dude – you just go 'ctrl-a' and the 'a', screen escapes and allows you to do the second a to get to the beginning of the line

      Reply
  2. Treenaks

    I’ve set my escape (that’s what it’s called) to Ctrl+X instead of Ctrl+A.

    Just put this bit in your ~/.screenrc:

    escape ^Xx

    I also have

    startup_message off

    in there.. because I know I’m starting screen :)

    Reply
  3. Treenaks

    Also, you could just send Ctrl+A using Ctrl+A a (or a Ctrl+X using Ctrl+X x in my case)

    Reply
  4. Byron Clark

    I always add these lines to my .screenrc:

    vbell on
    startup_message off
    shell -bash
    caption always “%{Gb}%3n %t %=[%l | %H | %c]”

    The last one gives a nice colored status bar that shows which screen windows is currently visible along with some other useful information like hostname, time, and load average.

    Reply
  5. Kris Marsh

    Open terminal 1, and start a screen up
    Open terminal 2, and type: screen -x

    Both screens point to the same session. Damn useful for teaching someone CLI!

    I set screen to run on all of my servers when sshing in, that way a ssh disconnection never interrupts whatever I’m doing.

    To show multiple sessions at the same time, you can “split” (Ctrl+a S) instead of new (Ctrl+a c). To switch between splits, you can use Ctrl+a .

    Ctrl+a A – allows you to rename the session name (useful to distinguish between sessions when you’ve invoked Ctrl+a “).

    Most importantly, Ctrl+a ? for help :-)

    Reply
  6. Mikael Eriksson

    This is what I have in my .zshrc. It makes the title of the terminal window change to “command (user@host dir)” when a command is running and “user@host dir” when not. It also changes the title of the virtual screen inside screen.

    To use it you’ll need to make sure the ^] and ^G is real and not written out like here.

    case $TERM in
    rxvt|*term)
    preexec () { print -Pn “\e]0;$1 (%n@%m %~)^G” }
    export PS1=”%{^]]0;%n@%m %~^G%}[%n@%m %~]%# ”
    ;;
    screen)
    preexec () { print -Pn “\ek$1 (%n@%m %~)\e\\” # Set screen title
    print -Pn “\e]0;$1 (%n@%m %~)^G” # Set window title
    }
    export PS1=”%{^]]0;%n@%m %~^G^]k%n@%m %~ESC\\%}[%n@%m %~]%# ”
    *)
    export PS1=”[%n@%m %~]%# ”
    ;;
    esac

    Reply
  7. ranf

    I like a neat status line at the bootom.

    $ cat .screenrc
    # Status at the bottom
    hardstatus alwayslastline
    hardstatus string ‘%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %D %Y-%m-%d %c:%s %{g}]‘

    (dunno how that will look once posted)

    Reply
  8. Nesta

    Not really a screen user, found it too complicated. If you simple want to detach sessions you should try dtach. It does everything I ever wanted screen to do…detach and reattach a session :)

    Start a session:
    dtach -A /tmp/irssi irssi

    Detach session:
    Ctrl+\

    Reattach session:
    dtach -a /tmp/irssi

    Reply
  9. Marius Gedminas

    I’ve tried to use screen a few times, but was always turned off. The drawing slowdown of vim/mutt in a local screen instance is very noticeable and unpleasant.

    However the killer bug was dropped keys when I tried to use vim from mutt from screen under ssh. When typing normally, about 1/5th of letters would be lost somewhere. The strangest thing that this happened with the combination of ssh+screen+mutt+vim. Drop any one of those programs and the problem went away.

    *shug*

    Here’s my .screenrc with the options I’ve found useful:

    startup_message off
    defscrollback 1000
    # C-A is too useful to take away.
    defescape ^bb
    escape ^bb

    # gimme back my scrollback in xterm!
    termcapinfo xterm|xterms|xs ti=\E7\E[?47l

    Reply
  10. Pingback: it’s about time» Blog Archive » links for 2007-05-05

  11. Pingback: Group Collaboration With Screen : Ubuntu Tutorials : Breezy - Dapper - Edgy - Feisty

  12. Pingback: Ubuntu | Christer Edwards: Group Collaboration With Screen

  13. Eric

    I tried screen, but the problem is that it changes my TERM from xterm to screen.

    Then when I try mc -x (mouse support on midnight commander) it doesn’t work.

    I have not been able to find a work around to this.

    Reply
  14. Pingback: Projeto ITA-Alumni » Guardando telas no terminal SSH do Linux

  15. Pingback: Sexy Sexy Penguins»Blog Archive » rtorrent - An introduction

  16. Pingback: anmsidblog :: GNU Screen :: September :: 2007

  17. Fedoux

    Hello
    I have make a profile in gnome-terminal, and in this profile i have put a custom-command

    –> “screen -S irssi; irssi”

    than i have make a new entry in my main-menu with an custom icon, thats start my custom gnome-terminal-profile.

    –> “gnome-terminal –geometry=1024×768+0+0 –window-with-profile=irssi-screen”

    thats all ;)

    bye

    Reply
  18. Gavin

    I have just discovered “screen” and I must say I agree with you. LOVE IT LOVE IT LOVE IT. Now I use it daily! Thanks on the advice for exiting screens tho! Seems so simple when you think about it!!!!

    Reply
  19. marc

    im looking for openup a new deatached screen session within a bashscript with a commandline like this..
    screen -d -m `TS=$(date +%c); logger “STARTSYNC $TS”; /usr/bin/rsync -az –progress /mnt/source /mnt/targed && logger ‘SYNC done!’ || logger ‘SYNC failed!';TS=$(date +%c); logger “ENDSYNC $TS” `;
    in this case it seems that no screensession is opened. promt just cames back after rsync.
    but i whant to get the prompt back after running my script. rsync shuld be places in a screen session and terminates if done. in this case i will be able to watch the progress of rsync if i login to my server while sync is running.

    Reply
  20. ondiz

    screen command is wonderful tnx!!!!
    Using it on mac.
    You shown me a very unknown command (I’m coming from MS world) and useful for my nix server!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *