Command-line gems: screen

Jul 10, 11:29 am

Whenever you open any kind of command-line prompt to enter commands and run programs, anything you start will last only as long as the session is kept running. When you quit the session, any task running within it ends too. This is because the shell is seen as the “owner� of the process, and when the owner is quit, any processes it started is also quit.

For example, if you open a GNOME Terminal window, start a download with wget, and then quit the Terminal Window, the wget download will end too. This is annoying if you’re sometimes over-eager to clean up the desktop and quit terminal windows, like me!

If you access your computer across a remote session, perhaps via SSH, and want to run a program that will take a long time to complete, this can become seriously annoying. You have to stay logged in until the program has completed.

But getting around this is easy and you can use the handy screen program. This isn’t specifically designed to be an aid to remote logins, but there’s no reason why it cannot be used in such a situation.

The screen program effectively starts shell sessions that stick around, even if the shell window is closed or the ssh connection is ended or lost. After logging in to the remote computer via ssh, you can start a screen session by simply typing the program name at the prompt:

screen

After pressing the spacebar as prompted to start the program, there won’t be any indication that you’re running a screen session. There’s no info bar at the bottom of the terminal window, for example. screen works completely in the background.

Let’s consider what happens when you detach and then reattach to a screen session. To detach from the screen session, press Ctrl+A and then D. You’ll then be returned to the standard shell and, in fact, you could now disconnect from your ssh session as usual. However, the screen session will still be running in the background on the remote computer. To prove this, you could log back in, and then type this:

screen -r

This will resume your screen session, and you should be able to pick up quite literally where you left off; any output from previous commands will be displayed.

To quit a screen session, you can either type exit from within it or press Ctrl+A, and then Ctrl+\ (backslash).

The screen program is very powerful and you can have many “screens” as you want, so can juggle many tasks effectively in the background. To learn more about screen, read its man page. To see a list of its keyboard commands, press Ctrl+A, and then type a question mark (?) while screen is running.



    1. screen is really handy. you dont even need to use it over ssh to get its benefits. at one point, i used screen just on the dekstop, before tabbed terminals became available.

      nice tip.


    1. screen says:

      Don’t forget C-a c to create a new window within screen, with C-a n, C-a p or C-a number to change windows. Or C-a “ to get a list of windows. Or just read the man page to learn of all the screen awesomeness.

      Just one more gem, put this in your .screenrc to use C-z instead of C-a as the command key:

      escape ^zz

      Very handy if you use emacs or readline-enabled programs like bash.



    1. Yep, screen is really nice. See how I’m using it here:

      productivity tools: screen


    1. matt says:

      nohup anyone?


    1. rob says:

      For anyone that dislikes C-a as the command key (e.g. emacss or bash users) one of the two following keys may be convenient. Put one of these lines in a file called ~/.screenrc:

      #to use C-] as the command key
      escape ^]\\

      or

      #to use C-\ as the command key
      escape ^\\\

      it’s useful to use different escape sequences on different machines so that ssh’ing from one to the other and running screen within screen doesn’t cause an aneurism.

      another useful customization is to show the current screens at the bottom of the page:

      hardstatus alwayslastline ” ] H]{= Bw} %w %=”

      notice that that has a ] as the first character; this helps me remember which escape key I’m using at the moment. On a machine with C-\ as the escape key, it should be:

      hardstatus alwayslastline ” \ H]{= Bw} %w %=”

      good luck.



    1. Matt – nohup is useful for quicker tasks but you have no control once it’s started, short of manually killing the command. screen lets you go right back to where you left off.

      I sometimes use nohup to start updatedb.



    1. Don’t forget about screen -x which can allow you to share a screen session with other users. Very useful for training or remote support.


    1. somellama says:

      Screen -x is great for multiple detached sessions, we do this on our game server.

      e.g. putting screen -x game-server1 before the executable code will automatically run the code in a session that is detached, then entering the same command on the command line will bring you into the screened session, hitting CTRL+A then CTRL+D will detach the session and allow it to continue in the background..

      Good stuff..


    1. Magnus says:

      Try to nohup bittorrent-console, and logout.
      The process will stall.



    1. Just a couple of nits to pick.

      “Whenever you open any kind of command-line prompt to enter commands and run programs, anything you start will last only as long as the session is kept running. When you quit the session, any task running within it ends too. This is because the shell is seen as the “ownerâ€? of the process, and when the owner is quit, any processes it started is also quit.”

      Ummm, kind of. When you talk about a “session”, I think you mean to say pty (pseudo teletype). Your description of what happens when you exit your shell is also not quite accurate.

      What happens is that when you start a shell, it gets assigned to a pty (which is then owned by you). If you somehow get disconnected from the pty (eg. network connection closes) it sends a hangup (HUP) signal to the shell that was assigned to it. When the shell receives a HUP, it typically exits and causes hangup signals to be sent to any child processes that were running. I say usually because some shells like bash automatically catch the HUP signal and don’t forward it to child processes. Unless the application developer installed a handler for the HUP signal, the default action is for the operating system to kill the process.

      If you’re not running bash, another way to prevent child processes (usually running in the background) from being killed is by using the nohup program.

      All that being said, I always install screen on my servers and use that to manage my terminal sessions. It’s especially nice to reconnect to a server and be right back to where you were before that power outage killed your network connection.



    1. Magnus, the reason that “nohup bittorrent-console” hangs is that it suspends itself waiting for input on standard-in (stdin). I believe that if you use the bourne shell, you can close stdin with something like, “nohup bittorrent-console 0>&- &”, and the process will continue to run.



    1. Great program and tips, my new favorite command for remote administration!



    1. Yes screen is a necessity for remote admin.
      Here are my tips




Add your comments

Please keep your comments relevant to this blog entry: inappropriate or purely promotional comments may be removed. To add hyperlink, please follow this example: "your link text":http://your.link.url