How to use screen to keep a long running process on Linux

How to use screen to keep a long running process on Linux

System administrators often need SSH or telnet to log in to a Linux server remotely, often running tasks that take a long time to complete, such as system backups, FTP transfers, and so on. Usually, we open a remote terminal window for each of these tasks because they take too long to execute. You must wait for them to complete, during which you can’t close the window or disconnect, otherwise, the task will be killed and everything will be abandoned.

GNU Screen is a free software developed by the GNU project for command line terminal switching. Users can simultaneously connect multiple local or remote command line sessions through the software and switch between them freely.

GNU Screen can be thought of as the command line interface version of the window manager. It provides a unified interface for managing multiple sessions and corresponding functions.

  • Session recovery

As long as the Screen itself is not terminated, sessions running inside it can be resumed. This is especially useful for users who are logged in remotely – even if the network connection is lost, the user will not lose control of the command line session that is already open. Just log in to the host again and execute screen -r to resume the session. Also, when leaving temporarily, you can also execute the split command detach to suspend Screen (switch to the background) while ensuring that the program inside is running normally. This is very similar to VNC in the graphical interface.

  • Multi-window

In the Screen environment, all sessions run independently and have their own number, input, output, and window cache. Users can switch between different windows through shortcut keys, and can freely redirect the input and output of each window. Screen implements basic text operations such as copy and paste, and provides a scrollbar-like function to view the history of window conditions. The window can also be partitioned and named, and it can also monitor the activity of the background window.

  • Session sharing

Screen allows one or more users to log in to a session multiple times from different terminals and share all the features of the session (for example, you can see the exact same output). It also provides a window access mechanism to password protect the window.

Here is the URL of official¬†screen user’s manual:¬†https://www.gnu.org/software/screen/manual/screen.html

Example commands in CentOS:

Create a screen session with the name lnmp

#screen -S lnmp

If you are prompted to find the command, we can run the install command:

yum install screen

Use the command:

yum install screen

View all screen sessions
Shortcut Ctrl+a+d Save the current screen session and return

#exit

Exit to close the screen session

#screen -wipe lnmp

Delete session named lnmp

#screen -wipe lnmp

Restore session with name lnmp

Like this post? Please share this:

Leave a Reply

Your email address will not be published. Required fields are marked *