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

The "3I" Standards for Successful Print Advertising

For many domestic advertisers, the creation and ev...

Use of Linux bzip2 command

1. Command Introduction bzip2 is used to compress...

How to check if a table exists in MySQL and then delete it in batches

1. I searched for a long time on the Internet but...

Detailed explanation of the code for implementing linear gradients with CSS3

Preface The gradient of the old version of the br...

MySQL 8.0.13 download and installation tutorial with pictures and text

MySQL is the most commonly used database. You mus...

N ways to center elements with CSS

Table of contents Preface Centering inline elemen...

Sample code of uniapp vue and nvue carousel components

The vue part is as follows: <template> <...

Navicat cannot create function solution sharing

The first time I wrote a MySQL FUNCTION, I kept g...

Steps to change mysql character set to UTF8 under Linux system

Table of contents 1. Check the MySQL status in th...

HTML implements read-only text box and cannot modify the content

Without further ado, I will post the code for you...

In-depth analysis of Flex layout in CSS3

The Flexbox layout module aims to provide a more ...

How to set the text in the select drop-down menu to scroll left and right

I want to use the marquee tag to set the font scro...

How to implement the singleton pattern in Javascript

Table of contents Overview Code Implementation Si...

Detailed explanation of how Zabbix monitors the master-slave status of MySQL

After setting up the MySQL master-slave, you ofte...