Using Screen Window Manager to Run Programs in Background

Posted by Oleksiy Kovyrin under Admin-tips · русский

Sometimes, when I speaking with Unix admins, I wondering, that they did not know anything about very useful UNIX tool – Screen window manager. That is why i decided to describe how I am using it in my job.

Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells. There is a scroll-back history buffer for each virtual terminal and a copy-and-paste mechanism that allows the user to move text regions between windows. When screen is called, it creates a single window with a shell in it (or the specified command) and then gets out of your way so that you can use the program as you normally would. Then, at any time, you can create new (full-screen) windows with other programs in them (including more shells), kill the current window, view a list of the active windows, turn output logging on and off, copy text between windows, view the scroll-back history, switch between windows, etc. All windows run their programs completely independent of each other. Programs continue to run when their window is currently not visible and even when the whole screen session is detached from the users terminal.

In every day Unix admin job, this tool may be interesting for running some curses-based jobs in background. For example, if you need to run full-screen bittorent client (BitTornado) :-) to download some files, you can not use generic way to run background jobs with nohup tool, because this software requires full-featured virtual terminal and can not be runned in background.

If you need to run some software in background, you need to do following steps:

  • Create empty screen session:

    1
    # screen -dR some_screen_name

  • Run your full-screen program:

    1
    # btdownloadmany .

  • Detach from terminal by pressing Ctrl+a, d on your keyboard.

Now, your program is being executed in full-screen background session. You can disconnect from the server and do anything you want. When you will need to look at its state, you can simply attach to background screen by typing:

1
# screen -dR some_screen_name

If you forgot your background session name, you can look at the sessions list by the following command:

1
2
3
4
5
# screen -list
There is a screen on:
        4330.some_screen        (Detached)
1 Socket in /var/run/screen/S-root.
#

As you see here, screen utility can be very useful for running background jobs on remote UNIX servers and, even, can replace old job backgrounding method with ugly constructions like

1
nohup command &> log.file &

.


Related posts:

  1. Using epoll() For Asynchronous Network Programming
  2. Using Nginx As Reverse-Proxy Server On High-Loaded Sites
  3. Using X-Accel-Redirect in Nginx to Implement Controlled Downloads
  4. Using Lighttpd, Mplayer/Mencoder and Flvtool2 to Implement Flash Video Streaming
  5. How to run GUI-programs on a server without any monitor

5 Responses to this entry

Jan says:

Very very useful! :)

Indeed: I though I had quite some Unix experience, but didn’t know about the existence of screen.

Sergey Shakhnov says:

Да – как раз то что искал. Спасибо!!! Весьма и весьма своевременно!

Tristan says:

awesome.

Great guide and real useful. Screen is perfect for my remote FreeBSD box running rtorrent. Thanks a bunch.