Very usefull screen command in Linux Print

  • screen, command, commands
  • 0

screen command in Linux provides the ability to launch and use multiple shell sessions from a single ssh session. When a process is started with ‘screen’, the process can be detached from session & then can reattach the session at a later time. When the session is detached, the process that was originally started from the screen is still running and managed by the screen itself. The process can then re-attach the session at a later time, and the terminals are still there, the way it was left.

Sometimes it is necessary to keep the command-line interface open for an extended period. In this time-frame, you need to make sure that the system does not time you out, or even if you have an internet issue, your session remains uninterrupted. The "screen" command is perfect for the occasion as it opens another shell, within the current session, allowing you to run applications or programs in the background. It is perfect if you want to start a download process that you estimate is going to take at least 2 hours. It is fantastic when you are executing a script that needs to run for an extended time.

Syntax:

screen [-opts] [cmd [args]]

Options:

  • -a: It force all capabilities into each window’s termcap.
  • -A -[r|R]: It adapt all windows to the new display width & height.
  • -c file: It read configuration file instead of ‘.screenrc’.
  • -d (-r): It detach the elsewhere running screen (and reattach here).
  • -dmS name: It start as daemon: Screen session in detached mode.
  • -D (-r): It detach and logout remote (and reattach here).
  • -D -RR: It do whatever is needed to get a screen session.
  • -e xy: It change the command characters.
  • -f: It make the flow control on, -fn = off, -fa = auto.
  • -h lines: It set the size of the scrollback history buffer.
  • -i: It interrupt output sooner when flow control is on.
  • -l: It make the login mode on (update /var/run/utmp), -ln = off.
  • -ls [match]: It display all the attached screens.
  • -L: It turn on output logging.
  • -m: It ignore $STY variable, do create a new screen session.
  • -O: It choose optimal output rather than exact vt100 emulation.
  • -p window: It preselect the named window if it exists.
  • -q: It quiet startup. Exits with non-zero return code if unsuccessful.
  • -Q: It commands will send the response to the stdout of the querying process.
  • -r [session]: It reattach to a detached screen process.
  • -R: It reattach if possible, otherwise start a new session.
  • -S sockname: It name this session .sockname instead of …
  • -t title: It set title. (window’s name).
  • -T term: It use term as $TERM for windows, rather than “screen”.
  • -U: It tell screen to use UTF-8 encoding.
  • -v: It print “Screen version 4.06.02 (GNU) 23-Oct-17”.
  • -x: It attach to a not detached screen. (Multi display mode).
  • -X: It execute as a screen command in the specified session.

Shortcut keys Options:

  • Ctrl-a + c: It create a new windows.
  • Ctrl-a + w: It display the list of all the windows currently opened.
  • Ctrl-a + A: It rename the current windows. The name will appear when you will list the list of windows opened with Ctrl-a + w.
  • Ctrl-a + n: It go to the next windows.
  • Ctrl-a + p: It go to the previous windows.
  • Ctrl-a + Ctrl-a: It back to the last windows used.
  • Ctrl-a + k: It close the current windows (kill).
  • Ctrl-a + S: It split the current windows horizontally. To switch between the windows, do Ctrl-a + Tab.
  • Ctrl-a + |: It split the current windows vertically.
  • Ctrl-a + d: It detach a screen session without stopping it.
  • Ctrl-a + r: It reattach a detached screen session.
  • Ctrl-a + [: It start the copy mode.
  • Ctrl-a + ]: It paste the copied text.

Using the "screen" command is very easy. All you need to do is log into your hosting account via SSH and then type:

screen 

Upon doing so, your current SSH session is going to appear as if it just reloaded. When this happens, you may start your long downloads or execute your long-running scripts. After doing so, to return to your first shell, you can press Ctrl + a + d sequence in quick succession. When you press this combination, you are going to receive the following output from the command-line:

[detached from 8924.pts-0.ger1] 

The string following the "detached from" is randomly generated, so do not pay much attention to it. What's important is the fact that you have now detached from the screen. All processes you started within the screen are running in the background. To return to your screen and check if the tasks you started have been completed, please type:

screen -rd 

This command is going to take you back to the "screen" immediately. To terminate it, please type "exit" while inside the "screen" itself. Doing this is going to generate the following output:

[screen is terminating] 

You will NO LONGER be able to return to this screen, so please, be mindful when exiting.

Sometimes, you need to start more screens at the same time. The "screen" command-line tool allows you to create as many shell sessions as you need to, and it gives you the option of naming them with specific names relevant to the tasks they are going to run. To do so, use the "-S" flag:

screen -S <screen_name>

We have created two screens within a shell, and we have given them individual names. Here is what it looks like:

[user@ger1 ~]$ screen -ls
There are screens on:
        12431.update_product_list       (Detached)
        12378.long_download     (Detached)
2 Sockets in /var/run/screen/S-user. 

To receive the list of screens, please use the "-ls" option:

screen -ls 

To select the screen, you want to return to please type in:

screen -r <screen_name>

For example, we would like to return to the download screen, thus we are going to type:

screen -r 12378.long_download 

Was this answer helpful?

« Back