Go2Linux | Linux Operating System

A site dedicated to: Linux Operating System

Linux screen command tutorial

Date: 2010-04-30 00:00:00 -0400

I have face this lots of times, I am connected to my server using ssh, then I start a download, a log processing or a bittorrent download. I realize that task is going to take a lot of time. What to do?, well usually one have leave the ssh session open, and the home computer on. What if the ssh session just drops? all work is done, and there is the need to start all over again.

I have used nohup command to execute commands after exit from the shell prompt.

But there are other options, one of them is screen.

From Wikipedia:

GNU Screen is a free virtual terminal multiplexer that allows a user to access multiple separate terminal sessions inside a single terminal window or remote terminal session. It is useful for dealing with multiple programs from the command line, and for separating programs from the shell that started the program.

With screen the user is able to use several multiple terminal over a unique ssh connection, and maybe the best part, is that you can "detach" the screen and leave it running (with all the programs started in it) until you connect to it again.

Let's see how to install and use Screen.

Installing

All Linux distributions should have it on its package manager, so just invoke it and install according to your Linux Distribution.

Using Screen

Start it from the command line:

screen

You should get a message like this:

GNU Screen is a free  virtual terminal multiplexer  that allows a user to access multiple separate terminal sessions inside a single terminal window or remote terminal session. It is useful for dealing with multiple programs from the command line, and for separating programs from the shell  that started the program.

Then just press space or return and you are in, it will appear a new empty screen, press <CRTL+a> release them, and press ?.

You will get this:

GNU screen screenshot

Press space to exit.

Detaching a screen

To detach a screen, press ctrl+a release and then d. then you can just exit from your ssh shell. When you want to recover the jobs running under that screen just re-attach it.

Attaching to sessions again

First review the list of screen sessions you have running.

screen -ls

There is a screen on:
        3946.pts-0.arch (Dettached)
1 Socket in /tmp/screens/S-ggarron.
To resume the session run:

screen -r 3946.pts-0.arch

Running multiple sessions You can creat multiple sessions by pressing ctrl+a release them and press c to create new sessions, and to alternate between them press: ctrl+a release them and press n for the next session, or p for the previous session.

If you liked this article please share it.

powered by TinyLetter

If you want to contact me in any other way, please use the contact page.