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

Summary of Common Mistakes in Web Design

In the process of designing a web page, designers...

How to remotely connect to MySQL database with Navicat Premium

The party that creates a new connection is equiva...

Detailed explanation of small state management based on React Hooks

Table of contents Implementing state sharing base...

MySQL log trigger implementation code

SQL statement DROP TRIGGER IF EXISTS sys_menu_edi...

JavaScript Canvas draws dynamic wireframe effect

This article shares the specific code of JavaScri...

Basic reference types of JavaScript advanced programming

Table of contents 1. Date 2. RegExp 3. Original p...

A brief discussion on MySQL count of rows

We are all familiar with the MySQL count() functi...

Practical record of solving MySQL deep paging problem

Table of contents Preface Why does limit deep pag...

Detailed explanation of Vue element plus multi-language switching

Table of contents Preface How to switch between m...

How to deploy springcloud project with Docker

Table of contents Docker image download Start mys...

Understanding and using React useEffect

Table of contents Avoid repetitive rendering loop...

Explanation of the problem of selecting MySQL storage time type

The datetime type is usually used to store time i...

Vue+thinkphp5.1+axios to realize file upload

This article shares with you how to use thinkphp5...

MySQL uses custom sequences to implement row_number functions (detailed steps)

After reading some articles, I finally figured ou...