Home  >  Article  >  Operation and Maintenance  >  A detailed introduction to the configuration method of keeping the process running after the SSH client is disconnected in Linux

A detailed introduction to the configuration method of keeping the process running after the SSH client is disconnected in Linux

黄舟
黄舟Original
2017-05-26 09:29:505455browse

In the cloud server ECS Linux system, usually when we execute some tasks that take a long time to run, we must wait until the execution is completed before disconnecting the SSH connection or closing the client software, otherwise it may cause execution Interrupt. This article introduces several methods to ensure that the program continues to run after the user logs out and logs in.

Use the management terminal to execute

You will log in to the local session (console) port of the server through the management terminal, and the program executed in this terminal will not be affected by the exit of the SSH login user. The specific operation method is as follows:

1. Log in to the server through the management terminal.

2. Execute the required program or script.

3. Next time you need to check the task execution status, just connect to the management terminal again to check.

Use nohup to execute

The role of nohup is as the name suggests, it prevents subsequent commands from responding to the hangup (SIGHUP) signal. In other words, after executing nohup through Remote login, the program will still execute normally even after logging out. Normally, the nohup command will be followed by an & character at the end, indicating that the command will be executed in the background. Only in this way can the command be executed in the background continuously.

Operation example:
1. The normal execution command is bash hello.sh, and the execution result is a small program that outputs one line per second:

A detailed introduction to the configuration method of keeping the process running after the SSH client is disconnected in Linux

2. Add nohup and & at the beginning and end of the command to become nohup bash hello.sh &. You can see that nohup outputs a line of information, and then Press the Enter key to jump back to the shell command line. At this time, the command has been executed in the background, and nohup redirects the output of the command to the nohup.out file in the current directory. Also note that nohup will output the PID of the corresponding program. The PID can be used to kill the process when the process needs to be interrupted.

A detailed introduction to the configuration method of keeping the process running after the SSH client is disconnected in Linux

3. Through tail -f nohup.out you can continuously view the output of nohup.out to achieve the effect of monitoring the program.

A detailed introduction to the configuration method of keeping the process running after the SSH client is disconnected in Linux

4. You can also use redirection in the command to change the output of the program to the file name you want, such as nohup bash hello.sh >hello .log &, the output of the program will be written to the hello.log file.

5. If the program does not exit automatically, you need to use the kill command to end the process. For example, you can use the command kill -TRM , where PID is the value output by nohup before, in this example the value is 1231.

Usage restrictions:

nohup is usually used to execute automated programs or scripts without intervention and cannot complete operations with interaction.

Use screen to execute

Installation sceen tool

Linux system does not come with the screen tool by default, you need to install it first:

  • CentOS series system: yum install screen

  • ##Ubuntu series system:

    sudo apt-get install screen

Usage

Introduction

1. Create a screen window

screen -S  name
# name可以设置为ssh、ftp,用于标注该 screen 窗口用途
# 示例:screen -S ftp

2. List the screen process and enter The required screen

screen -ls  ##列出 screen 进程列表

is as shown below


A detailed introduction to the configuration method of keeping the process running after the SSH client is disconnected in Linux

and then perform the required operations, such as running scripts, executing programs, etc.

As shown below: Create an ftp connection and download the transfer file in the background

A detailed introduction to the configuration method of keeping the process running after the SSH client is disconnected in Linux

3. Exit and save

After the above ftp operation example starts the transfer, Type the Ctrl+a keys in the window and then press the d key to exit the SSH login, but it will not affect the execution of the screen program.

4. Save the session to continue execution

You can use the screen function to manage remote sessions. Overview of operation steps:

  • Normal SSH login to the server

  • Create a screen window

  • Required for execution Task

  • When you need to temporarily interrupt and exit, press Ctrl +d to save and exit

  • When you need to continue working, log in to the server via SSH again, and then execute directly

    screen -r -d Restore the session.

The above is the detailed content of A detailed introduction to the configuration method of keeping the process running after the SSH client is disconnected in Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn