How to keep process running after SSH client disconnects?

This article describes how to keep the process running after disconnecting the SSH client in the ECS instance of the Linux system.

Scenes to be used

In the Linux system, when executing some long-running tasks, you must wait for the completion of the execution before disconnecting the SSH connection or closing the client software, otherwise the execution may be interrupted. This article describes two ways to keep programs running after you log out.

Method 1: Use nohup to execute commands

nohup, you can run the command to ignore the SIGHUP signal. Therefore, even if you log out, the program will continue to execute. Usually, the & character is added at the end of the nohup command to execute the command in the background. A specific example is shown below.

Description nohup is usually used to execute non-intervention automation programs or scripts, and cannot complete operations with interaction.

1. Execute the following command to output one line of information per second.


2. Use nohup to execute the command.

Add nohup and & at the beginning and end of the command, you can see that nohup outputs a line of information, and then press Enter 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 current directory. in the nohup.out file. At the same time, nohup will output the PID of the corresponding program, and the PID can be used to end the process when the process needs to be interrupted.

Explanation You can also use redirection in the command to change the output of the program to the file name you want, as shown below, then the output of the program will be written to the hello.log file.

nohup bash >hello.log &
nohup bash &

3. Execute the following command to continuously view the output of nohup.out to achieve the effect of monitoring the program.

tail -f nohup.out

4. Execute the following command to end the process.

kill -TRM [$PID]

Note [$PID] is the value output by the previous nohup command.

Method 2: Use screen to execute commands

GNU Screen is a software developed by the GNU project for command line terminal switching, which can be used as a command line interface version of the window manager. As long as the Screen itself is not terminated, the sessions running inside it can be resumed, which is especially suitable for performing some long-running tasks.

1. Install the screen tool.

The Linux system does not have the screen tool by default and needs to be installed first.
• The installation commands for CentOS series systems are as follows.

Note The CentOS 6 and CentOS 8 operating system versions have reached the end of life (EOL). According to community rules, the content of the source address of CentOS 6/8 has been removed. When you continue to use the default configured source address in the CentOS 6/8 system, an error will occur. It is recommended that you switch the source address of CentOS 6/8 before proceeding.

• Ubuntu series system installation

sudo apt-get install screen

2. Run the following command to create a screen window.

screen -S [$Name]

3. Execute the following command to list the screen windows.

screen -ls

4. When you need to run a script or execute a program, just add screen before the command.

5. Press Ctrl+a+d at the same time to log out of SSH without affecting the operation of the screen program.

6. To continue working, log in to the instance and execute the following command to restore the session.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us