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 nohup command on Linux system

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

Share

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

This article is about how to use the nohup command on the Linux system. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The nohup command can be run by ignoring the suspend signal, and the output information of the program being run will not be displayed to the terminal.

Use permissions for all users

Syntax format nohup Command [Arg... ] [&] Parameter description: Command: command to be executed.

Arg: some parameters, you can specify the output file.

&: let the command execute in the background, and the command will still be executed after the terminal exits.

Case demonstration: 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 uses 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 I find the pid corresponding to 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 Thank you for your reading! This is the end of this article on "how to use nohup commands in Linux system". I hope the above content can be of some help to you, so that you can learn more knowledge. 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

Development

Wechat

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

12
Report