The use of nohup, &, and redirection in the Linux shell background startup command

 1. How to use nohup and &


1.1, nohup (do not hang up)


nohup is an abbreviation for no hung up, meaning not to hang up.

When using Linux client tools such as Xshell to remotely execute Linux scripts, sometimes due to network problems, the client will lose connection, the terminal will be disconnected, and the script will end unexpectedly half way through. In this case, you can use the nohup command to run the command, even if the client is disconnected from the server, the script on the server can still continue to run.

nohup syntax format:

nohup  command  [arg...]


illustrate:

  1. In addition to being unable to perform input operations (such as inputting commands, newlines, hitting spaces, etc.) ,
  2. Standard output is saved to the nohup.out file.
  3. After closing the client, the command will still run without hanging up .

For example:

After executing the nohup sh test.sh script command, the terminal cannot receive any input, and the standard output will be output to the nohup.out file in the current directory. Even after closing xshell and exiting, the current session continues to run.

1.2, & (interactive)
& grammar format:

command   [arg...]   &



illustrate:

  1. Can perform input operations (such as inputting commands, newlines, hitting spaces, etc.), and can perform interactive input and output operations.
  2. Standard output is saved to the nohup.out file.
  3. But after closing the client, the program will stop immediately .

For example:

After executing the sh test.sh & script command, close the xshell, and the script program will stop immediately.

1.3. Use nohup and & together (no hang up, interactive)
syntax format:

nohup   command  [arg...]  &



illustrate:

  1. Can perform input operations (such as input commands, newlines, blank spaces, etc.), and can perform interactive input and output operations,
  2. Standard output is saved to nohup.out,
  3. Commands will still run after the client is closed.

Example:
After executing the nohup sh test.sh & command, the input operation can be performed, and the standard output log is written to the nohup.out file. Even if xshell is closed and the current session is exited, the script command continues to run.

The input and output problem has been solved, is it perfect? In fact, there is still a problem that has not been resolved, please read below!

2. Log redirection>


The default name of the log file mentioned above is nohup.out. If you modify the name of the log file, redirection is used. The symbol is > and the syntax format is

 > logFile



illustrate:

> is the symbol for redirection.
logFile is the name of the log file, preferably in English and numbers.
At this time, the grammatical format used by nohup, & and > together:

nohup  command >logFile  &



Example:

nohup  start.sh >aa.log  &



Note: After executing the above command, you can input or run in the background, and the running log is output to the aa.log log.

3. Handling of error messages

nohup  command >logFile  &



Although it solves the problem of input and output and can run in the background, there is another item that the error information cannot be output to the log file. To solve this problem, you need to add the command 2 > file.

Standard output and error messages are used at the same time, and the syntax format is as follows:

>logFile1   2 >logFile2



Some people may wonder, what does 2 mean? Please read below.

3.1. Symbols of Linux standard input, output, and error messages


Symbols for Linux standard input, output, error messages:

0 means stdin (standard input) standard information input;
1 means stdout (standard output) standard information output;
2 means stderr (standard error) error information;
/dev/null means empty device file. Use this parameter if you don't want to output any logs.
Let's review the above example again:

>logFile1   2 >logFile2



> logFile1: namely 1 >logFile1, 1 is the standard information output, which is the default and can be omitted, logFile1 is the name of the log file.

2 >logFile2: 2 is the error message, that is, the error message will be output to the logFile2 file.

At this point, understand the meaning of 2!

3.2. Error message and standard output are in the same file
If you want to put error message and standard output in the same file, use 2>&1. The syntax is as follows:

>logFile   2>&1



illustrate:

>logFile indicates that the standard information is output to the logFile file;
2>&1 indicates that 2 (error information) is redirected and output to 1 (standard output).
The common use of the two means that both 2 (error information) and 1 (standard output) are output to the same file (logFile).

3.3. Thinking: What should I do if I don’t want to output log information?
Tip: /dev/null means empty device file. Use this parameter if you don't want to output any logs.

4. Comprehensive use (recommended)
To sum up, the most complete function, the recommended syntax is as follows:

nohup  command  >logFile   2>&1  &



Example:

nohup  start.sh > mySysLog.log  2>&1   &



Explanation: After the command is executed, the standard output (1) and error information (2) are written to the mySysLog.log file.

5. Knowledge Expansion


5.1. Clear nohup.out directly without stopping the service


If the script continues to run, the nohup.out log will continue to grow, but the hard disk capacity of the log is limited, how to reduce the size of the log file?
Note, do not delete the log file directly, otherwise the service will not be able to output the log, and the service will stop running immediately due to abnormality, which is the most serious production accident.

There are two ways to directly clear the nohup.out file without stopping the service:

# 第1种:
cat /dev/null > nohup.out

# 第2种:
cp /dev/null nohup.out



5.2. Only record logs with a relatively high warning level


There are too many output logs, and nohup.out grows very fast. For unimportant logs, you can not record them, and choose to only record logs with a relatively high warning level.

# Only output error messages to the log file, other logs are not output

nohup ./program > /dev/null   2>error.log  &



5.3, do not want to output logs


I don't want to output logs, and I don't want any logs, as long as the service can run normally.

# No log output
nohup ./program > /dev/null 2>&1 &
—————————————————
 

Guess you like

Origin blog.csdn.net/u010919402/article/details/127900305