Server Administration & Management

Screen command

Many times as a Linux sysadmin, you will need to run multiple commands at once. You are probably doing this by opening multiple ssh sessions but there is a better way to do it ! Also as a sysadmin you probably had to run a command or script that is taking hours to finish like a rsync. This usually requires you to keep the ssh session open as if you close it the command or script will also be closed. If you ever faced any of those problems then it’s probably time to learn about the screen command.

Screen – makes it possible to run multiple full-screen pseudo-terminals from one real terminal, and lets you manipulate and save your screen input and output, copy and paste between windows.

First of all make sure that you have screen installed. If you don’t you can install it using yum, apt-get or any other package manager that you might have on your server.

# yum install screen

Now type in:

# screen

This will start a new screen for you. You could also name you screen using the -S option. Something like:

# screen -S 1

In this case I named the screen “1?.

The screen will look like any other ssh window. You can now type in your command that you want to run.

If you want to detach from the screen and still keep the command running you can do this by pressing:

Ctrl-a d (that is press Ctrl-a, release and press d)

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

If you want to reatach to a screen you simply type in:

# screen -r

Other options that you have when you are in a screen are:

1. To create a new screen:

Ctrl-a c

2. To switch between screens:

Ctrl-a n

3. And many others.


January 30, 2011 - Posted by | Tips & Tricks, Unix/Linux | , , ,

1 Comment »

  1. Thank you for showing this data. I believe that it is most useful and definately share it to close friends, in addition. If you go on publishing about this subject I will surely carry on checking out your upcoming articles.

    Comment by amazing water blog | June 16, 2011 | Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: