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.
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.
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:
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.
3. Through tail -f nohup.out you can continuously view the output of nohup.out to achieve the effect of monitoring the program.
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
Usage restrictions:
nohup is usually used to execute automated programs or scripts without intervention and cannot complete operations with interaction.
Linux system does not come with the screen tool by default, you need to install it first:
CentOS series system: yum install screen
sudo apt-get install screen
screen -S name # name可以设置为ssh、ftp,用于标注该 screen 窗口用途 # 示例:screen -S ftp
screen -ls ##列出 screen 进程列表
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!