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

What are the Linux nohup commands?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article will explain in detail what the Linux nohup commands are, and the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Guide at work, we often run a very important program, sometimes the program needs to run for hours, or even days, at this time, if we exit the terminal, or the network connection is not good, then the program will be aborted. And this situation is certainly not what we want to see, we hope that even if the terminal is closed, the program can still run.

The nohup command is an acronym for the English phrase no hangup, meaning not to hang up, that is, the program does not exit. This command causes the program to ignore the HUP signal and ensures that the program works properly. The HUP signal, which may be unfamiliar to some people, is a signal sent to the process associated with it when the terminal is aborted, and the process will stop running when it receives this signal. So if you don't want the process to be killed by this signal, you can ignore it. And that's what nohup commands to do.

In this article, we will describe the specific use of the nohup command in detail.

Basic syntax of nohup command

The basic syntax of the nohup command is as follows:

$nohup command arguments or $nohup options

If you want more information about the use of nohup, check out its help page:

$nohup-help

Start a program using the nohup command

If you need to run a program that keeps running even after the corresponding Shell is exited, you can run the program using nohup:

$nohup command

When the program starts, the corresponding log output of the program and its error log will be recorded in the nohup.out file, which is usually located in the home directory or the current directory.

The output of the redirector program

If I do not want to save the output of the program in the home directory or the current directory, I want to save it in the path I specified and customize the file name, what should I do? At this point, we can use the redirect operation.

For example, I now have a script myScript.sh that I want to save its output to the output directory in my home directory, with the file name myOutput.txt, which can be run like this:

$nohup. / myScript.sh > ~ / output/myOutput.txt

Use the nohup command to start a program in the background

If you want the program to run in the background, you can add the & symbol. But after running in this way, the program disappeared without a trace. To get the program back to the terminal, use the fg command.

The output log of this command will be saved in the nohup.out file, which you can view using cat or other commands. The number 8699 in the second line represents the process number corresponding to the command, that is, pid. We can use the ps command to find this process.

Running multiple programs at the same time using nohup

If you need to run multiple programs at the same time, you don't have to run them one by one, just use the & & symbol. For example, if you want to run mkdir and ping,ls commands at the same time, you can run them like this:

$nohup bash-c 'mkdir files & & ping-c 1 baidu.com & & ls' > output.txt

Stop the process running in the background

As mentioned above, the nohup command combined with the & symbol allows the process to run in the background, even if the terminal is closed. At this point, what do you do if you want to terminate the process?

The simplest is the kill command, which I believe you have used many times.

$kill-9 PID

So how do you find the corresponding pid for the process? We can use the ps command.

$ps aux | grep myScript.sh

Or you can use the pgrep command.

Next, use the kill command to terminate the process.

$kill-9 14942 that's all about Linux nohup commands. I hope the above can be helpful to you and learn more. If you think the article is good, you can share it for more people to see.

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