Introduction to Linux screen background tasks

Introduction to Linux screen background tasks

Screen is mainly used for long-time-consuming operations, such as compiling large package programs. If you do not use screen, if you disconnect ssh during the compilation process, the compilation will also be interrupted.
But using screen is equivalent to a background task. Even if the compilation window is closed, the compilation will continue to execute.

My current job is to use it in the compilation environment of the source code of the Android system.
Usually just before get off work, create a screen task, and come to see the compilation results the next day.

This article only introduces the common operations of screen.

1. Install screen:

Check if installed:

rpm -q screen

This command to check the installation does not necessarily use all environments.
The easiest way is to enter screen to see if it can be recognized, and you will know whether screen is installed in the environment.

Installation:
take centos as an example, use apt for debian

yum install screen


Administrator privileges are required to install, but the general environment will be installed.

Two, create screen

When performing time-consuming operations, open the screen session first.

The session name is to define a name for this screen session, so that it is convenient to return to this session next time.
Of course, you can also directly execute screen without adding any parameters, and return to this session through the session id.

1、screen

The system will set a session name by default, don't worry, because each session name has time to rub.

2. screen -S session name

After starting a new screen session, perform the operations you need.

3. Exit the current screen session:

Enter the command: exit

After logging out, the session is over and cannot be entered.

4. Get a list of all sessions:

Enter the command: screen -ls

This command will return the id and session name of all sessions
. For example:

First_Test$ screen -ls

There are screens on:
        186256.project0817      (2021年08月17日 20时15分46秒)   (Attached)
        110759.20210814 (2021年08月14日 12时39分25秒)   (Detached)
        258777.pts-0.R740-861   (2021年08月05日 18时22分58秒)   (Detached)
3 Sockets in /var/run/screen.


Here you can see that there are three screen tasks, Attached means that there is a window open, and Detached means that the window is not opened.
If you exit, you will not see the information.

186256.project0817 //The front of the first decimal point represents the process id, and the back represents the process name. The default generated process name may include a decimal point.

5. Enter a session

screen -r session_name           # 回到session_name这个session

Enter the previous screen session (if you are already in the session, you will not be able to enter):

For example, to enter session 258777.pts-0.R740-861, you can use the command:


screen -r 258777.pts-0.R740-861
screen -r pts-0.R740-861

6. Return the session task to the background and return to the previous interface

In each screen session, ctrl+a+d is a more commonly used operation.
Its function is to suspend the task and put it in the background for execution, and it will return to the state before entering the screen. Even logout will not affect it at this time.

7. Grammar and common commands

1. Complete grammar

screen [-AmRvx -ls -wipe][-d <作业名称>][-h <行数>][-r <作业名称>][-s ][-S <作业名称>]

Parameter Description

-A adjusts 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 viewport.
-m Force a new screen job to be created even if the screen job is already in the job.
-r <job name> Resume offline screen jobs.
-R Attempt to resume offline jobs first. If no offline job can be found, create a new screen job.
-s Specifies the shell to execute when creating a new window.
-S <job name> Specifies the name of the screen job.
-v Display version information.
-x Resume a previously offline screen job.
-ls or –list displays all current screen jobs.
-wipe Check all current screen jobs and delete screen jobs that are no longer available.

2. Common screen parameters

screen Create a new task at the current time
screen -S yourname -> create a new session called yourname
screen -ls -> list all current sessions
screen -r yourname -> return to the session of yourname
screen -d yourname -> remote detach a certain session
screen -d -r yourname -> end the current session and return to yourname session

Summarize:

The three most commonly used ones are:
screen Create current task
screen -ls View previous tasks
screen -r XXX Restore last task session

Others are basically not used.

Mutual encouragement: time will always pass, so don't let the future leave regrets.

Guess you like

Origin blog.csdn.net/wenzhi20102321/article/details/122890088