How to use screen command

Screen is one useful tool that can be used to run processes in background. Usually this utility is not shipped with CentOS distributions but, you can easily install it anytime. Linux OS also has inbuilt features to run background processes but, I personally find screen much easier and more elegant rather than using (&, fg, bg, jobs).

First make sure that screen is installed, you can verify if the package exists by running: rpm -qa | grep screen.

If the software is not installed, run yum install screen:

Screen command

How to install screen command

To open a new session simply type screen. You can also specify a session name by running screen -S  session_nameA new terminal will be opened in which you can execute whatever you want. For this exercise execute top:

top command

How to use top command

Screen can be now be manipulated using Ctrl+a and a letter or symbol. For example, you can execute Ctrl+a then ? to view the help screen:

Screen command

Screen command help menu

To return to the shell without closing the current top execution type Ctrl+a d. This command will detach but the shell window will still be opened. 

Ctrl+a c is used to create a new window

Ctrl+a k will kill the current window

Ctrl+a w will list all screen windows

You can start another screen window and execute something else in parallel. If you type screen -r or screen -ls you’ll see a list with detached sessions:

Screen command

List detached screen sessions

To enable a detached session execute screen -R session_name or  screen -R session_pid command.Because we’ve created a session named ‘ppscu’, I’ll execute screen -R ppscu. Note that if only one session is in detached state, screen -R command will enable that session automatically.

If a screen session is attached to another terminal you can use the screen -d -R session_name or screen -d -R session_pid to attach it to the current terminal. This command will first detach it from the old terminal before attaching it to the current one. For this example I’ve used screen -d -R 4438.ppscu because I’ve opened multiple screen windows within the ppscu terminal:

Screen command

Detach screen window and attach to current terminal

These are the main operations that you can use with the screen command. Note that you ca further discover its features by looking into its man page (man screen command).


Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s