How to Install and Use screen

From Acenet Knowledgebase
Jump to: navigation, search

If your server is running a Linux based operating system other than CentOS/RedHat, the installation process of screen may be a little different. All other information provided should be the same for using on Linux based operating systems.

Ever have a SSH connection drop out on you when you are doing something important? You lose any information you may have had because the connection dropped, and now you have to start all over. Wouldn't it be nice if you could prevent this? Well, you can by using screen.

screen is a handy tool that allows you to use multiple virtual terminals in Linux. You can process separate tasks in each virtual terminal. This tool is highly praised by Linux Technicians, Linux Engineers, and any Linux webmaster. Acenet's System Administrators use screen pretty much every day.

Installing screen

screen should already be installed on your server. You can verify that it is installed on your CentOS/RedHat server by running this command:

which screen

If screen is installed, you will receive this path output:

/usr/bin/screen

If screen is not installed, run this command to install it:

yum install screen

Using screen

To enter screen, simply run this command:

screen

This will take you to a screen virtual terminal, you can verify you are in screen by looking at the top left corner of your SSH window for [screen 0: bash].

To kill and exit a screen virtual terminal, run this command from within the screen virtual terminal you're wanting to kill and exit:

exit

Other than the commands "screen" and "exit", you must signal to screen you are issuing a command by pressing Ctrl-A on your keyboard followed by a letter. For example, here is how to detach from screen:

Ctrl-A D

I am telling screen I am issuing a command by pressing Ctrl-A. I am then telling screen to detach by pressing D. Detaching from screen is useful because doing so does not kill the virtual terminal. So, any commands you executed within the screen virtual terminal will continue to run until they have completed or the screen virtual terminal is killed.

After you have detached from screen, you can re-attach to a screen virtual terminal by running the command:

screen -r

Now let's say you have multiple screen virtual terminals running and you want to re-attach to a specific one. In order to do this, you need to know the session ID of the screen virtual terminal you're wanting to re-attach to. To find out the session ID, run the following command:

screen -ls

This will show all of the running screen virtual terminals and their session IDs. Here is an example:

root@server [~]# screen -ls
There are screens on:
547391.pts-1.server       (Detached)
549766.pts-1.server       (Detached)
2 Sockets in /var/run/screen/S-root.

There are 2 screen virtual terminals in the above example. The numbers are the screen session IDs for the respective virtual terminals.

Now that you have the screen session ID, run this command to re-attach to the screen virtual terminal:

screen -r FULLSESSIONID

replacing FULLSESSIONID with the screen virtual terminal's actual session ID

To view screen's manual, run this command:

man screen