Screen command and usage in Linux

Screen command and usage in Linux

Screen Introduction

Screen is a free software developed by the GNU project for command line terminal switching. Users can use this software to connect to multiple local or remote command line sessions at the same time and switch between them freely. GNU Screen can be thought of as a command-line interface version of a window manager. It provides a unified interface and corresponding functions for managing multiple sessions.

Official website: http://www.gnu.org/software/screen/

GUN Screen:

In the Screen environment, all sessions run independently and have their own number, input, output, and window buffer. Users can switch between different windows using shortcut keys and can freely redirect the input and output of each window.

Screen Syntax

$> screen [-AmRvx -ls -wipe][-d <job name>][-h <number of lines>][-r <job name>][-s ][-S <job name>] -A Resize all windows to the size of the current terminal. -d <job name> Take the specified screen job offline. -h <number of lines> specifies the number of buffer lines for the window. -m Forces the creation of a new screen job even if one is already in progress. -r <job name> Restore the offline screen job. -R Try to recover offline jobs first. If the offline job cannot be found, a new screen job is created. -s specifies the shell to be executed when creating a new window. -S <job name> specifies the name of the screen job. -v Display version information. -x Restore the previously offline screen job. -ls or --list displays all current screen jobs.

Importance of screen

The importance of screen lies in the fact that it can run on the server all the time without stopping as long as the server is not down or without power outage. In fact, backend engineers often encounter the situation where they need to process some data by executing scripts, but this script may take half a day or several days to execute. Screen is the best choice. After a while, log in to the server to check the running status and whether it has been completed. It is not finished and continues to run.

Common parameters of screen

The screen command has many parameters. We will only look at the ones that are commonly used.

parameter illustrate
ls List all current conversations
S Capital S, create a new session named xxx
r Connecting to a process session by sequence number
d detach disconnect a session
D Same as -d command, but will logout the user in the screen

In each screen session, all commands start with ctrl+a.

parameter illustrate
ctrl+ad Detach the session, drop the current screen session to the background for execution, and return to the state before entering the screen. The screen continues to execute, and even if you log out, it will not affect the execution of the background
ctrl+ak Force close the current window
ctrl+az Put the current session into the background and call it back with the fg command

Use of screen

First simulate a script code that takes a long time to execute

test.php
<?php
sleep(10000);
echo 111;
?>

The program sleeps for 10000 seconds and then outputs 1111

First create a screen, then execute it and log out

screen -S test
php test.php

Create a screen named test and execute test.php after entering the screen. Then let the script execute in the screen all the time, and press Ctrl+ad to let the script execute in the background of the screen.

screen -S test
[detached from 3934.test]

exit Log out, wait for a while and then log in again to check if the script is still executing

ssh 
screen -ls
There are screens on:
 3934.test (02/25/2020 10:27:06 PM) (Detached)

The screen ID of test is 3934. Connect to the screen and enter

screen -r 3934
php test.php

You can see that the test.php script is still executing.

Execute ctrl+az to execute the script in the background, and then use fg to bring it to the foreground

[1]+ Stopped screen -r 3934
Execute fg
php test.php

Let's look at screen -d and -D

Reopen a window, ssh, and execute

screen -d 3934
[3934.test detached.]

Then check the previous window to see if the screen is disconnected.

screen -r 3934
[remote detached from 3934.test]

Look again, -D, in the first window

screen -r 3934

In the second window, execute -D

screen -D 3934
[3934.test power detached.]

Looking at the first window again, you have been forced to log out.

screen -r 3934
[remote power detached from 3934.test]
Connection to 127.0.0.1 closed.

As a developer, you only need to know three commands to meet more than 90% of the usage scope in normal development.

screen -ls
screen -S
screen -R
ctrl+ad

Common screen operations

Create a session (-m forced):

screen -dmS session_name
# session_name session name

Close the session:

screen -X -S [session # you want to kill] quit

View all sessions:

screen -ls

Enter the session:

screen -r session_name

Summarize

This is the end of this article about the screen command in Linux and how to use it. For more information about the linux screen command, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of screen command usage in Linux
  • Detailed explanation of screen command in Linux system command
  • Detailed explanation of screen commands in Linux

<<:  jQuery implements simple button color change

>>:  MySQL 5.7.27 installation and configuration method graphic tutorial

Recommend

Write a shopping mall card coupon using CSS in three steps

Today is 618, and all major shopping malls are ho...

Detailed explanation of MySQL from getting started to giving up - installation

What you will learn 1. Software installation and ...

Pure js to achieve the effect of carousel

This article shares the specific code of js to ac...

js+css to realize three-level navigation menu

This article example shares the specific code of ...

Example code for CSS columns to achieve two-end alignment layout

1. Going around in circles After going around in ...

Basic structure of HTML documents (basic knowledge of making web pages)

HTML operation principle: 1. Local operation: ope...

MySQL 8.0.21 installation and configuration method graphic tutorial

Record the installation and configuration method ...

html page!--[if IE]...![endif]--Detailed introduction to usage

Copy code The code is as follows: <!--[if IE]&...

Detailed explanation of MYSQL large-scale write problem optimization

Abstract: When people talk about MySQL performanc...

Basic operations of mysql learning notes table

Create Table create table table name create table...

CSS and HTML and front-end technology layer diagram

Front-end technology layer (The picture is a bit e...