Analysis on the Examples and Differences of Running Background Tasks with nohup and screen in Linux

  • 2021-07-01 08:38:28
  • OfStack

Using SSH terminals (such as putty and xshell) to connect to Linux servers to run time-consuming tasks may take hours or even days to complete. At this time, the terminals are occupied, and we have to do other tasks. Therefore, we need to put this type of time-consuming tasks in the background to run, just record the logs. There are the following common methods to realize this requirement.

command &

Enter command at the terminal & Run, at which time the abort command using Ctrl + C will be invalid because of the & Run SIGINT1 signal immunity in the background, and the program will continue to run. But if you shut down the terminal directly, the process will be aborted. Visible, using the & Process running in the background will be aborted by SIGHUP2 signal, but if you exit the terminal normally with exit command, the program will continue to run in the background without aborting.

nohup command

The standard output will be appended to nohup. out in the current directory by default. If the nohup. out file in the current directory is not writable, the output will be redirected to $HOME/nohup.out In the file.

Then, it is easy for us to think of combining these two features and running the command like this:

nohup command > /home/user/myfile.log 2>&1 &

This will execute this task in the background 1 until it is complete, and direct the program standard output (1) and standard error (2) to the/home/user/myfile. log file.

screen Tools

screen can be understood as a session window management tool. For 1-like use, we only need to use the following operations:


#  Use yum Installation screen
yum install screen
#  Create 1 A person named test Session window of 
screen -S test
#  Temporary departure window 
Ctrl+a d( That is, press and hold Ctrl , and then press a,d)
#  View Existing Session Window 
screen -ls
#  Entry window 
screen -r test
screen -r  Process ID
#  Close a window 
exit
#  Window switching 
Ctrl+a c  : In the current screen Create a window in a session 
Ctrl+a w  Window list 
Ctrl+a n  : Under 1 Windows 
Ctrl+a p  : Above 1 Windows 
Ctrl+a 0-9  : In the 0 Window and number 9 Switch between windows 

With the screen tool, We can create a window on the terminal, Then run the required command, then leave temporarily, and then you can continue to do other things. Of course, you can also use other terminals to connect to the server and use screen-r commands to connect to the required window. You will find that Program 1 is running directly, and exiting the terminal will not affect the programs running on those temporarily left terminals.

Test:

We write a test script test. sh:


#!/bin/sh
for ((i=1; i<1000; i++))
do
  d=`date '+%Y-%m-%d %H:%M:%S'`
  echo "$d  No. 1  $i  Secondary output ;"
  tt
  sleep 2s
done
Bash

Use only & Run in the background:


[root@localhost test.cc]# ./test.sh > test.log 2>&1 &
[1] 15037
[root@localhost test.cc]# ps -aux | grep test.sh
root   15037 0.0 0.0 113180 1424 pts/0  S  16:10  0:00 /bin/sh ./test.sh
root   15045 0.0 0.0 112712  992 pts/0  S+  16:10  0:00 grep --color=auto test.sh
Bash

You can see the 15037 process running;

If the terminal is shut down directly, logging stops, indicating that the process stops executing with the end of the terminal session;

However, after repeated tests, it is found that if you use exit command to exit the terminal normally, and then connect to the terminal, use & The running program continues to run, that is, & The command is the same as nohup effect, and the test environment I use here is CentOS 7.6.

Using nohup & Run


[root@localhost test.cc]# nohup ./test.sh > test.log 2>&1 &
[1] 14349

Test results, directly shut down the terminal or use the exit command to exit the terminal, the program will continue to execute.

Running with screen


#  Create a window 
screen -S test
#  Run the command, you can use the following one here & , but the program will occupy the window 
./test.sh > test.log 2>&1 &
#  Temporary departure 
Ctrl + a d

At this time, you can directly close the terminal, then reconnect, and find that the program is still running. If you want to find the window connection:


#  Existing windows 
[root@localhost ~]# screen -ls
There is a screen on:
  18048.test (Detached)
1 Socket in /var/run/screen/S-root.
#  Entry window 
[root@localhost ~]# screen -r test

After entering, you will find that the window still keeps the state before temporarily leaving, including the programs and commands entered before. When you enter the exit command in the window to exit the window, if you do not use the previous command behind it & The program will be aborted, because this is a foreground program relative to the test window, and the program will naturally be aborted when the window exits.

Because of the temporary departure function of screen, it can keep the foreground program running continuously in the temporary departure window, which is equivalent to running in the background compared with the real terminal.

Program Termination (interrupt) signal, issued when the user types the INTR character (usually Ctrl-C) to inform the foreground process group to terminate the process

Issue at the end of terminal connection, usually closing the terminal, notifying each process group in 1session

Summarize


Related articles: