Geek Culture
Published in

Geek Culture

Run Background Program with Screen

screen image is taken from google

We often need SSH or telnet to remotely log in to the Linux server, and often run some tasks that take a long time to complete. During this period, we cannot close the window or disconnect, otherwise, the task will be killed and everything will be aborted or, the network will suddenly be interrupted and the task is not completed. At this time, we can open the screen session to solve it.

The screen command can separate the current window from the task. Even if we are offline, the server is still running tasks in the background. When we log in to the server again, we can read the window thread and reconnect to the task window.

The screen is a full-screen window manager that can multiplex a physical terminal between multiple threads. There is the concept of a session on the screen. Users can create multiple screen windows in a screen session. Each screen window is like operating a real SSH connection window.

Install GNU Screen

GNU Screen is available in the default repositories of most Linux operating systems.

# To install GNU Screen on Arch Linux, run:$ sudo pacman -S screen# On Debian, Ubuntu, Linux Mint:$ sudo apt-get install screen# On Fedora:$ sudo dnf install screen# On RHEL, CentOS:$ sudo yum install screen# On SUSE/openSUSE:$ sudo zypper install screen

The main functions

  • Session Resume: As long as the Screen itself is not terminated, the sessions running inside it can be resumed. This is particularly useful for users who log in remotely-even if the network connection is interrupted, the user will not lose control of the command line session that has been opened. Just log in to the host again and execute screen -r to resume the session. Similarly, when leaving temporarily, you can also execute the detach command to suspend the Screen (switch to the background) while ensuring the normal operation of the program inside. This is very similar to VNC under the graphical interface.
# To start the session
$ screen -S screen_name
# To resume the session or Reattach session enters a screen process
$ screen -r
# If there will be multiple screen running then
$ screen -r screen_name
# To detach the session
$ screen –d screen_name
# View all screen sessions
$ screen -ls

After entering the screen session, you can create multiple windows in the session and manage the windows. The management command starts with ctrl+ a

ctrl + a + c: create a new window (create)ctrl + a + n: switch to the next window (next)ctrl + a + p: switch to the previous window (previous)ctrl + a + w: list all windowsctrl + a + A: Rename windowctrl + a + d: detach the current sessionctrl + a + [1-9]: switch to the specified window (1-9 is the window number)ctrl + d: Exit (close) the current window
  • 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, etc. It also provides a scroll bar-like function to view the history of the window status. The window can also be partitioned and named, and the activity of the background window can be monitored.
  • Session Sharing: Screen allows one or more users to log in to a session multiple times from different terminals and share all the characteristics of the session (for example, you can see exactly the same output). It also provides a mechanism for window access authority, which can password protect the window.

New window

There are 3 ways to create a new window:

$ screen #In this way, you can create a new window and enter a window, but then the window has no name, and cannot distinguish between them

$ screen -S name #This creates a new window named name and merges it into the window

For example screen -S count creates a new window called count and enters

$ screen command #This creates a new window and executes the command in the window, also without a name

For example screen python ./ Create and execute program

Other Useful Screen Commands

# Terminate Screen session, If the session is no longer needed, just kill it. To kill the disconnected session named senthil:screen -r sample -X quitor:screen -X -S sample quitor:screen -X -S 29415 quit
Ctrl + a :list all conversationsCtrl + a 0 :switch to session number 0Ctrl + an :switch to the next sessionCtrl + ap :switch to the previous sessionCtrl + a S :Split the current area into two areas horizontallyCtrl + al :Split the current area into two areas verticallyCtrl + a Q :Close all sessions except the current sessionCtrl + a X :close the current sessionCtrl + a \ :Terminate all sessions and terminate ScreenCtrl + a? :Show key bindings. To log out, press enter #### Lock session
# Screen has an option to lock the session. To do this, press Ctrl + a and x. Enter your Linux password to lock it.Screen used by sk on ubuntuserver.Password:# Record sessionYou may want to record everything in the Screen session. To do this, just press Ctrl + a and H.# Alternatively, you can use the -L parameter to start a new session to enable logging.screen -LFrom now on, all the activities you do in the session will be recorded and stored in a file named screenlog.x in the $HOME directory. Here, x is a number.You can use the cat command or any text viewer to view the contents of the log file.

Kill the session window

If you want to close an extra window, there are 3 ways:

kill -9 threadnum For example, in the 2637 above, kill -9 2637 can kill the thread, and of course it kills the window

Use Ctrl a +k to kill the current window and the programs running in the window

Use Ctrl a and enter the quit command to exit the Screen session. It should be noted that this exit will kill all windows and exit all running programs

Clear dead windows

When the window is killed, you can use screen -ls to see (???dead) behind the window, indicating that the window is dead, but still occupying space. At this time you need to clear the window

$ screen -wipe #Automatically clear dead windows

This kind of window is clear~

If there is no open session, you will see the following output:

$ screen -ls

No Sockets found in /run/screens/S-sk.

For more details, please refer to the man page:

$ man screen

I hope you enjoyed the small session. If you have any comments or questions or suggestions, please join the forum discussion below!

Thanks for the support( follow or Clapp)!!!!!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Budhdi Sharma

Budhdi Sharma


A developer is responsible for developing a framework and system applications for devices powered by the Android operating system.