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 linux uses the nohup command to make the program run in the background

2025-01-16 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 to make the program run in the background", 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 to make the program run in the background" this article.

The way to run a program from the background in the linux operating system is to use the nohup command.

Under Unix/Linux, for example, if you want a program to run in the background, many of them use & at the end of the program to make the program run automatically.

For example, to run mysql in the background:

The code is as follows:

/ usr/local/mysql/bin/mysqld_safe-user=mysql &

But adding many programs does not make a daemon like mysqld, maybe the program is just an ordinary program, usually this kind of program uses & ending, but if the terminal is closed, then the program will be closed.

In order to be able to run in the background, you can use the command nohup. For example, if a test.php needs to be run in the background and wants to run regularly in the background, then use nohup:

The code is as follows:

Nohup / root/test.php &

Enter in shell and prompt:

[~] $appending output to nohup.out

The standard output of the original program is automatically redirected to the nohup.out file in the current directory, which plays the role of log.

But sometimes there will be problems in this step. When the terminal is turned off, the process will be shut down automatically. Check the nohup.out

To see that the service shuts down automatically at the moment the terminal is shut down.

After consulting the Red Flag Linux engineer, he was also puzzled. After executing on my terminal, the process he started was still running after the terminal was closed. In the second demonstration to me, I found that I and he operate the terminal when a detail is different: he is when the shell prompted nohup success, but also need to press any key on the terminal back to the shell input command window, and then type exit in the shell to exit the terminal; and I am every time after the successful execution of nohup off the program button to close the terminal. So the session corresponding to the command will be cut off at this time, causing the process corresponding to nohup to be notified that it needs to be shutdown together. Someone didn't notice this detail as much as I did, so I wrote it down here.

Attached: nohup Command reference

Nohup command

Purpose: run the command without hanging up.

Syntax: nohup Command [Arg... ] [&]

Description: the nohup command runs the command specified by the Command parameter and any related Arg parameters, ignoring all SIGHUP signals. Use the nohup command to run the program in the background after logging out. To run the nohup command in the background, add & (the symbol for "and") to the end of the command.

Whether or not the output of the nohup command is redirected to the terminal, the output is appended to the nohup.out file in the current directory. If the nohup.out file in the current directory is not writable, the output is redirected to the $HOME/nohup.out file. If there is no file that can be created or opened for append, the command specified by the Command parameter cannot be called. If standard error is a terminal, the specified command is written to all outputs of standard error as standard output is redirected to the same file descriptor.

Exit status: this command returns the following exit values:

The command specified by the Command parameter can be found but cannot be called.

An error occurred in the 127th nohup command or could not find the command specified by the Command parameter.

Otherwise, the exit status of the nohup command is the exit status of the command specified by the Command parameter.

Nohup command and its output file

Nohup command: if you are running a process and you don't think it will end when you exit your account, you can use the nohup command. This command can continue to run the corresponding process after you exit your account / close the terminal. Nohup means not to hang (no hang up).

The general form of the command is:

The code is as follows:

Nohup command &

Use the nohup command to submit a job

If you submit a job using the nohup command, by default all output from the job is redirected to a file named nohup.out, unless another output file is specified:

The code is as follows:

Nohup command > myout.file 2 > & 1 &

In the above example, the output is redirected to the myout.file file.

Use jobs to view tasks.

Using fg% jobnumber is to take the task to the foreground for execution. If you want to close the task, press the Ctrl+c key combination after getting it, but if you want to pause the task, you can press the Ctrl+z key combination to put the task in the paused state.

[root@wangdm ~ 22:51 # 81] # jobs

[2] + Stopped dd if=/dev/zero of=/dev/null bs=8k count=100000000

[3]-Stopped dd if=/dev/zero of=/dev/null bs=8k count=100000000

If you want to suspend the task to continue to execute later, you can use bg% jobnumber so that the paused task continues to run in the background and the state will become running

Someone may have noticed that the two task numbers 2 and 3 above me are followed by'+ 'and' -', respectively. What does this'+ 'mean? This'+ 'represents the task called by default in the background under the current window, which sounds a little awkward to do an experiment:

The two tasks we saw above are both in a stopped state, right? under normal circumstances, to let the task continue to run in the background is the bg% jobnumber command. I am now doing the following. I directly typed in bg Task 2 and was activated, that is to say, under this window, the background activated Task 2 with'+'by default, and now 3 becomes with'+'. You can repeat the operation. Now you should understand it. Fg is the same as this. Friends who are interested can try it.

[root@wangdm ~ 23:09 # 86] # bg

[2] + dd if=/dev/zero of=/dev/null bs=8k count=100000000 &

[root@wangdm ~ 23:09 # 87] # jobs

[2]-Running dd if=/dev/zero of=/dev/null bs=8k count=100000000 &

[3] + Stopped dd if=/dev/zero of=/dev/null bs=8k count=100000000

[root@wangdm ~ 23:09 # 88] # bg

[3] + dd if=/dev/zero of=/dev/null bs=8k count=100000000 &

[root@wangdm ~ 23:11 # 89] # jobs

[2]-Running dd if=/dev/zero of=/dev/null bs=8k count=100000000 &

[3] + Running dd if=/dev/zero of=/dev/null bs=8k count=100000000 &

There are also two commonly used ftp tools, ncftpget and ncftpput, which can upload and download ftp in the background, so that you can use these commands to upload and download files in the background.

The above is all the contents of the article "how linux uses the nohup command to make the program run in the background". 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