Several methods of running background tasks under Linux

Follow the WeChat public account "This is how you should learn cloud computing" and search for "001" to get 154 pages of Linux learning notes compiled by Krypton Gan.

Today I will introduce to you how to execute several background tasks in Linux.

1. Introduction of the problem

The most intolerable thing for programmers is that when using the terminal, ssh is often disconnected due to network, closing the screen, executing CTRL+C, etc., causing the running program to exit, causing our work to fall short.

The main reason behind this is the above-mentioned related operations. By default, the shell will send an interrupt signal to the process associated with the terminal session, causing the process to exit with the terminal. In order to clarify this problem, we must first understand the two interrupt signals:

1) sigint: signal interrupt, ctrl+c will send this signal and actively close the program

2) sighup: signal hang up, close the terminal, the network is disconnected, closing the screen will send this hang up signal.

Today I will introduce to you how to execute several background tasks in Linux to avoid the above problems.

2.& symbol

This is a way to put & at the end of the execution command to make the started program ignore the sigint signal. At this time, executing ctrl+c to close will not close the process. However, when the screen is turned off and the network is disconnected, the process will still cause the process to exit.

sh test.sh &

3.nohup command

nohup (no hang up) means not to hang up the operation. Running the command with nohup can make the command execute permanently. It has nothing to do with the user terminal. Disconnecting SSH does not affect the operation. nohup captures SIGHUP and ignores it, so The process will not exit when the screen is turned off, the network is disconnected, etc., causing ssh to be interrupted. But ctrl+c can close the process. Therefore, in most cases, programs started using nohup and & at the same time cannot be closed by ctrl+c or closing the terminal. By default all output is redirected to a file named nohup.out.

The basic usage format of the nohup instruction is:
nohup Command \[ Arg ... \] \[ & \]

Example

The background does not interrupt execution./test.sh, stdout is output to out.log, and stderr is output to err.log.

nohup ./test.sh > out.log 2>err.log  &

The relevant numbers have the following meanings:

  • 0 – stdin (standard input),

  • 1 – stdout (standard output), obviously nohup command > out.log is equivalent to nohup command 1> out.log, which is the default behavior.

  • 2 – stderr (standard error)

Maybe you will also see this writing method, which means that stderr is also redirected to stdin.

nohup ./test.sh > out.log 2>&1  &

4.ctrl + z、jobs、fg、bg

What if our program does not use & and nohup when starting up? Do we need to execute ctrl + c first to terminate the process executing in the foreground and then restart it? Obviously there is a good way!

4.1 ctrl + z

Put a job process that is executing in the foreground into the background and pause it. In terminology, it is suspended. The execution is as follows:

[1]+ Stopped ./test.sh

4.2 jobs

Check how many commands are currently running in the background. [jobnumber] is the job number.

jobs  
[1]+ Stopped ./test.sh   
[2]+ Running ./test2.sh &

4.3 bg

Continue to run the suspended (suspended) job process in the background, for example, put job No. 1 (./test.sh) in the background to run, pay attention to the &

bg 1  

[1]+ ./test.sh  &

4.4 fg

Move the job process in the background to the foreground to continue running, for example, move job No. 2 (./test2.sh &) to the foreground.

fg 2   

./test2.sh

5.screen command

5.1 Introduction

If the above method implements front and back task scheduling through Linux-related commands, then screen provides another idea.

Non-human version: GNU Screen is a free software developed by the GNU Project for command line terminal switching. Users can use this software to connect to multiple local or remote command line sessions at the same time and switch between them freely. GNU Screen can be thought of as the command line interface version of a window manager. It provides a unified interface and corresponding functions for managing multiple sessions.

Human version: We can roughly think of screen as a virtual terminal software that directly starts another background program in the Linux system to take over (maintain) your terminal session. When the terminal SSH you are directly connected to is disconnected, it Still let the program think that your ssh connection is continuously connected, so that the process will not exit after receiving an interrupt signal.

5.2 Installation

yum install screen

5.3 Use

1) Create a new session

screen -S yourname -> 新建一个叫yourname的session

2) List all current sessions

screen -ls

3) Resume the session (return to the session yourname)

screen -r yourname

4) detach a session

screen -d yourname -> 远程detach某个session 
screen -d -r yourname -> 结束当前session并回到yourname这个session

5) Delete session

screen -S pid-X quit

Guess you like

Origin blog.csdn.net/weixin_41692221/article/details/131381729