nohup output to the specified file Linux nohup implements the command to run in the background and output or record to the specified log file set the log result file name to redirect to a file standard error standard error output direction

Background running command: nohup

nohup command: nohup means no hang up (no hang up). If you have a process running that you don't think will end when you log out, use the nohup command. This command ignores all SIGHUP signals and can continue to run the corresponding process after you log out of the account/close the terminal.

The general format of this command is:

nohup yourcommand &

# yourcommand: Start the object command. Various parameters required by the command can be followed.
# & refers to running in the background, but when the user exits (suspends), the command will automatically exit. The combination of nohup and & can realize background running without hanging up.

Record screen output to log file

nohup yourcommand 2>&1 &

# 0 – stdin (standard input), 1 – stdout (standard output), 2 – stderr (standard error);
# 2>&1 is to redirect standard error (2) to standard output (&1) , standard output (&1) Then redirected input to the log file.


If you want to output the log to another file, you can add a file path parameter. as follows:

nohup yourcommand >myout.log 2>&1 &

Where myout.log is the file name to save the output;

这是一条在Linux/Unix系统下的命令,用于在后台运行一个命令并将输出重定向到一个文件中。下面对每个部分进行解释:

nohup: 这是一个命令,意为"no hang up",用于让命令在后台一直运行,即使用户退出登录或终端关闭。
yourcommand: 这是你要在后台运行的命令。

2>&1: 这个部分是将标准错误输出重定向到标准输出,也就是将命令的错误信息也输出到文件中。
&: 这个符号是将整个命令放入后台运行,以便你可以在终端中继续输入其他命令,而不用等待该命令执行完毕。

综合起来,这条命令的作用是在后台运行一个命令,并将命令的输出(包括标准输出和标准错误输出)重定向到一个文件中,同时不会受到用户退出登录或终端关闭的影响。

>You can use the redirection symbols and on the command line 2>to redirect the output of a command (including standard output and standard error output) to a different file. For example, you can redirect standard output and standard error to separate files with the following command:

yourcommand > stdout.log 2> stderr.log

The above command will yourcommandredirect the standard output of the command to stdout.loga file and the standard error output to stderr.loga file.

If you want to combine standard output and standard error output into one file, you can use &>redirection notation, for example:

yourcommand &> output.log

The above command will yourcommandcombine the command's standard output and standard error output into output.loga file.

You can also customize the name and path of the output file, just follow the redirection symbol with the file name or file path. For example:

yourcommand > /path/to/stdout.log 2> /path/to/stderr.log

The above command will yourcommandredirect the standard output of the command to /path/to/stdout.loga file and the standard error output to /path/to/stderr.loga file


Real-time monitoring of log output content command: tail

The tail command is a good partner of the nohup command. With the -f parameter, you can monitor the information added to the log file in real time.

tail -f myout.log

tail -f is equivalent to –follow=descriptor, tracks according to the file descriptor, when the file is renamed or deleted, the tracking stops

tail -F is equivalent to –follow=name --retry, tracks according to the file name, and keeps retrying, That is, after the file is deleted or renamed, if the same file name is created again, tailf will continue to track

tailf, which is equivalent to tail -f -n 10. Unlike tail -f, if the file does not grow, it will not access the disk file , so tailf is especially suitable for tracking log files on those portable machines, because it reduces disk access and saves power.


Whether the monitoring program is running normally: ps or ps -ux

The ps command can also be used in conjunction with the nohup command to display the status of the current process (process). You can monitor whether the background program is running normally or has hung up.

ps -ef|grep yourcommand 
# The -ef parameter displays all commands, together with the command line parameters at startup

Welcome to pay attention to WeChat: Shengxin Little Doctor

If only the nohup command is used in this way, the output of the program will be redirected to a nohup.out file by default. If we want to output to a specified file, we can additionally specify the output file:

nohup ./test > myout.txt 2>&1 &

&

This method is very simple, just add an "&" symbol after the command, as follows:

./test &

In this way, the test program runs in the background. However, this processing is not enough, because although the program is running in the background, the log is still continuously output to the current terminal. Therefore, to make the terminal completely quiet, the log should also be redirected to the specified file:

./test >> out.txt 2>&1 &

2>&1 refers to redirecting standard error to standard output, so both standard error and standard output are redirected to the specified out.txt file, and the terminal is completely quiet from then on.

But be careful when doing this, if the Test program needs to receive data from the standard input, it will wait there and will not run any further. So you need to receive data from standard input, then this method is best not to use.

Now that the program is running in the background, how do we find it? It's easy, there are two ways:

1. jobs command

The jobs command can check how many jobs are currently running in the background.

jobs -l

This command can display the PID of all tasks, and the status of jobs can be running, stopped, terminated. But if the task is terminated (kill), the shell removes the task's process ID from the list known to the current shell environment.

2. ps command

ps aux | grep test

nohup command

After adding an & symbol at the end of the command, the program can run in the background, but once the current terminal is closed (that is, exit the current account), the program will stop running. So if we want to exit the current terminal, but want the program to run in the background, how should we deal with it?

In fact, this kind of demand is very common in reality. For example, if you want to compile a program remotely to the server, but the network is unstable, once the connection is disconnected, the compilation will be stopped, and the compilation needs to be restarted, which is a waste of time.

In this case, we can use the nohup command. nohup means not to hang up (no hang up). The general form of this command is:

nohup ./test &

If only the nohup command is used in this way, the output of the program will be redirected to a nohup.out file by default. If we want to output to a specified file, we can additionally specify the output file:

nohup ./test > myout.txt 2>&1 &

In this way, both the nohup command and the & symbol are used, and the standard output/error is redirected to the specified directory.

After using nohup, many people just ignore it. In fact, it is possible that the command will end by itself when the current account exits or ends abnormally. Therefore, after using the nohup command to run the command in the background, you need to use exit to exit the current account normally, so as to ensure that the command is always running in the background.

Guess you like

Origin blog.csdn.net/qq_52813185/article/details/128600702