Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the & and nohup commands in Linux

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "Linux & how to use the nohup command", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Linux & how to use the nohup command" this article.

We use the following test program to simulate a program that produces a large number of log, which prints the sentence "Hello world!" every second:

# include # include # include int main () {fflush (stdout); setvbuf (stdout, NULL, _ IONBF, 0); while (1) {printf ("Hello world!\ n"); sleep (1);}}

Now, we want a quiet world, do not have a large number of log on the terminal, we require test programs to run in the background.

# # & # #

This method is as simple as adding a "&" symbol after the command, as follows:

. / test &

In this way, the test program runs in the background. However, this is not enough, because although the program is running in the background, the log is still constantly output to the current terminal. Therefore, to keep the terminal completely quiet, you should also redirect the log to the specified file:

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

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

Note, however, that if the Test program needs to receive data from standard input, it will wait there and will not run any further. So you need to receive data from standard input, so it's best not to use this method.

Now that the program is running in the background, how do we find it? Quite simply, there are two ways:

1. Jobs command

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

Jobs-l

This command shows that the PID,jobs status of all tasks can be running, stopped, Terminated. But if the task is terminated (kill), shell removes the process identity of the task from the list known to the current shell environment.

2. Ps command

Ps aux | grep test

# # nohup Command # #

After adding an & symbol to the end of the command, the program can run in the background, but once the current terminal is closed (that is, exiting the current account), the program will stop running. What if we want to exit the current terminal but want the program to run in the background?

In fact, this kind of requirement is very common in reality, for example, if you want to compile the program remotely to the server, but the network is unstable and the compilation is aborted as soon as you drop the line, you need to restart the compilation, which is a waste of time.

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

Nohup. / test &

If you only use the nohup command like this, the output of the program will be redirected to a nohup.out file by default. If we want to export to the specified file, we can specify another output file:

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

This takes a multi-pronged approach, using both the nohup command and the & symbol, while redirecting standard output / errors to the specified directory.

After using nohup, many people just ignore it, in fact, it is possible that when the current account abnormally exits or ends, the order will end on its own. So after using the nohup command to run the command in the background, you need to use exit to exit the current account normally to ensure that the command runs in the background all the time.

The above is all the contents of the article "how to use the nohup command in Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report