https://net2.com/how-to-use-the-screen-command-on-linux-to-keep-your-remote-task-running-when-the-connection-drops
A
lot of Linux users carry out a long running task on a remote
destination without taking into account the possibility that the
connection might break. In such situations, notwithstanding financial
losses for large and small corporations,
they will lose their work as well as their time since when they try to
reconnect again, the remote task is no longer running.
Fortunately, Linux provides a tool called ‘screen’ which enables these remote sessions to keep running even when the connection drops. Indeed, when a given process is started with this utility, i.e. ‘screen’, it can be detached from the corresponding session and then when the connection is resumed for instance, the process that was originally started from the ‘screen’ is still running and can rejoin the session at a later time, .i.e users can pick up where they left off with their last command or task.
In
simple terms, when a user starts a screen session and opens up multiple
virtual terminals within that session, the running processes will not
be affected by a disconnection event.
Users have the ability to open as many screens as they need, switch between them, list them, kill them, detach them, copy and paste text between them and even reconnect to them.
All windows execute or manage their own programs independently of each other. When a window is not visible, its corresponding programs continue to run even when the full screen session is detached or decoupled from the user’s terminal.
screen –version
As you can see from the snapshot above, screen is not installed on our Ubuntu 18.04. You can install it by running the apt command :
sudo apt install screen
screen
You will be greeted with the following welcome message :
This will create a screen session, open a new window and finally start a shell in that window.
You may want to read: How to use scp command to transfer files securely using ssh on Linux
screen -ls
The highlighted number above, .i.e 8585, identifies the PID of the screen session.
If you type in the command below :
ps -e | grep 8585
You will see that it is indeed a screen session process.
Since
it is good practice to choose a memorable session name to remember what
screen is executing what command, run the command below:
screen -S command_name
Where command_name is the command that would be run by the session. Otherwise you can just choose a name for the session per se as shown below :
When you hit enter, a second screen session will be created. To see the number of currently active screen sessions , use the command you used earlier :
Notice that if you quit the terminal altogether and then open it up, you will see that the screen session is still running.
If case you need to quit the terminal, but you want your tasks to continue running in the screen session, you can detach the screen manually in order to reconnect with it later using Ctrl+a d.
screen -r Screen_PID
Once you hit enter, you will automatically get reattached to the screen session you had indicated.
You can also, instead of providing the PID, use the name (or the first few characters of the name) of the screen session, for instance:
screen -r mysec
Where mysec is the beginning of the name of the second screen session called: mysecondcreensession.
screen -ls
Now we want to quit session with PID 8585 as shown above. To do this we use the command below :
screen -X -S 8585 quit
where you can see that the screen session with PID 858 was terminated.
Let us now attach to the only screen session left, i.e. with PID 10313 :
In order to kill the current screen session from within it, press Ctrl+a k. You will be prompted with the question :
Go ahead and choose y. This will terminate the screen (actually this will kill the window, but since the screen has one window, it will get terminated).
In order to exit a one-window screen, you could simply type in the exit command which will take you back to the standard bash prompt.
You can also display the screen windows using ‘Ctrl-a :’
Here you can scroll up or down in order to move between your desired windows and then hit enter once you decide to land on the selected window.
You can also use Ctrl-a N to move to the next screen or Ctrl-a P to return to the previous screen.
screen -d -r your_detached_ression
This is helpful when you want to continue your work after a disconnection event for instance without losing the detached screen session context (running processes , window output).
How to run a command with screen
When you have a command to that you want to run using screen, issue the command below :
screen -d -m your_command
For instance to run a Python script:, issue the command below:
screen -d -m python your_python_script
To learn more about Gnu screen, visit the screen user’s manual page.
Fortunately, Linux provides a tool called ‘screen’ which enables these remote sessions to keep running even when the connection drops. Indeed, when a given process is started with this utility, i.e. ‘screen’, it can be detached from the corresponding session and then when the connection is resumed for instance, the process that was originally started from the ‘screen’ is still running and can rejoin the session at a later time, .i.e users can pick up where they left off with their last command or task.
What is screen ?
The ‘screen’ utility is a window manager that is used to multiplex a physical terminal between multiple processes or interactive shells. When the screen command is called, a single window will show up where the user can work normally.Users have the ability to open as many screens as they need, switch between them, list them, kill them, detach them, copy and paste text between them and even reconnect to them.
All windows execute or manage their own programs independently of each other. When a window is not visible, its corresponding programs continue to run even when the full screen session is detached or decoupled from the user’s terminal.
Installing Linux screen
On most Linux distros, the screen utility is already pre-installed. To check if screen is installed on your Linux system , type in the following command for Ubuntu and similar distros:screen –version
As you can see from the snapshot above, screen is not installed on our Ubuntu 18.04. You can install it by running the apt command :
sudo apt install screen
Start using screen
In order to start your first screen session, type in screen in your terminal:screen
You will be greeted with the following welcome message :
You may want to read: How to use scp command to transfer files securely using ssh on Linux
List of all currently opened screens
To find the list of all currently opened screens including those that are running in the background, issue the command below in your terminal :screen -ls
The highlighted number above, .i.e 8585, identifies the PID of the screen session.
If you type in the command below :
ps -e | grep 8585
You will see that it is indeed a screen session process.
Creating a named screen
From the snapshot below, the title of the session is not very descriptive.screen -S command_name
Where command_name is the command that would be run by the session. Otherwise you can just choose a name for the session per se as shown below :
When you hit enter, a second screen session will be created. To see the number of currently active screen sessions , use the command you used earlier :
Detaching from a screen
In order to detach yourself from the current screen, Press Ctlr+a d;If case you need to quit the terminal, but you want your tasks to continue running in the screen session, you can detach the screen manually in order to reconnect with it later using Ctrl+a d.
Reattaching to a screen
In order to reattach to a screen session, issue the command below in which the PID of the detached session would need to be provided :screen -r Screen_PID
Once you hit enter, you will automatically get reattached to the screen session you had indicated.
You can also, instead of providing the PID, use the name (or the first few characters of the name) of the screen session, for instance:
screen -r mysec
Where mysec is the beginning of the name of the second screen session called: mysecondcreensession.
How to quit an active session ?
In case you want to kill a screen session, you would need first to display the list of active screen sessions in order to identify the session to terminate :screen -ls
Now we want to quit session with PID 8585 as shown above. To do this we use the command below :
screen -X -S 8585 quit
where you can see that the screen session with PID 858 was terminated.
In order to kill the current screen session from within it, press Ctrl+a k. You will be prompted with the question :
Go ahead and choose y. This will terminate the screen (actually this will kill the window, but since the screen has one window, it will get terminated).
In order to exit a one-window screen, you could simply type in the exit command which will take you back to the standard bash prompt.
Creating multiple windows in a screen
To create several windows within your existing screen, simply press Ctrl-a followed by c. In order to see the list of windows in your current screen session, press Ctrl-a followed by w.Here you can scroll up or down in order to move between your desired windows and then hit enter once you decide to land on the selected window.
Renaming a window
If you want to rename a window, make sure you are already on the correct window that you want to rename and press Ctrl-a A. This will display the prompt below which enables you to rename the window :Switching between screen windows
When more than one screen window is opened, you can switch between them by pressing Ctrl-a followed by space or the shell number, .i.e. just press Ctrl-a 0, Ctrl-a 1, Ctrl-a 3 if you have three windows and so forth.You can also use Ctrl-a N to move to the next screen or Ctrl-a P to return to the previous screen.
Splitting windows
You have the possibility to split your windows instead of switching between them in order to have a bird view of your work. This can be achieved by pressing Ctrl-a followed by S or | for horizontal or vertical splitting. To move between panes, use Ctrl-a followed by tab. Here is how splitting a screen session will look like :Resuming a disconnected session
In case you want to resume a detached session to continue your work, run the command :screen -d -r your_detached_ression
This is helpful when you want to continue your work after a disconnection event for instance without losing the detached screen session context (running processes , window output).
How to run a command with screen
When you have a command to that you want to run using screen, issue the command below :screen -d -m your_command
For instance to run a Python script:, issue the command below:
screen -d -m python your_python_script
The screen configuration file
When you first call screen, it reads its actual configuration parameters from the file /etc/screenrc and ~/.screenrc if it is available. You can alter the default screen settings using the .screenrc file.Conclusion
In this tutorial, you were introduced to the Gnu screen utility. Now you should be able to use the screen tool, create screen windows, navigate between them, detach and resume screen sessions as well as customize your screen display using the .screenrc file.To learn more about Gnu screen, visit the screen user’s manual page.
No comments:
Post a Comment