The nohup command in the Linux system guide

  
                

In Linux system operation, some programs are disconnected during operation for various reasons. This requires the nohup command in Linux. Use this command to keep the program running in the background. Let's learn. Methods

In linux operating system has been running a program from the background, it 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 let 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 a lot of programs is not a daemon like mysqld. Maybe the program is just a normal program. Generally, this program ends with & but if the terminal is closed, the program will be closed.

In order to run in the background, you can use the nohup command. For example, if there is a test.php that needs to run in the background and you want to run it regularly in the background, then use nohup:

The code is as follows:

nohup /root/test.php &

After the carriage return in the shell 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 acts as a log.

But sometimes there is a problem in this step. When the terminal is closed, the process will be automatically closed. View nohup.out can be

to see that the service is automatically closed when the terminal is closed.

After consulting the Red Flag Linux engineer, he was also unable to understand. After executing on my terminal, the process he started was still running after the terminal was closed. When I showed it to me in the second time, I found out that I had a different detail from his operation of the terminal: after prompting nohup in the shell, he still needs to press any key on the terminal to return to the shell input command window, and then Exit the terminal by typing exit in the shell; and I close the terminal by clicking the Close button directly after the nohup execution succeeds. . Therefore, the session corresponding to the command will be disconnected at this time, and the process corresponding to nohup is notified that it needs to be shut down together. This detail has not been noticed by me, so I will record it 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 associated Arg parameters, ignoring all hangups (SIGHUP) signal. Run the program in the background with the nohup command after logging out. To run the nohup command in the background, add & (the symbol for ”and”) to the end of the command.

Regardless of whether 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 current directory's nohup.out file is not writable, the output is redirected to the $HOME/nohup.out file. If no files can be created or opened for appending, the command specified by the Command parameter is not callable. If the standard error is a terminal, then all outputs that write the specified command to the standard error are redirected as standard output to the same file descriptor.

Exit Status: This command returns the following exit values:

126 You can find but not call the command specified by the Command parameter.

127 The nohup command has an error or cannot 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 feel that the process will not end when you log out of the account, you can use the nohup command. This command can continue to run the corresponding process after you log out of the account/close the terminal. Nohup means no hang up.

The general form of the command is:

The code is as follows:

nohup command &

Submit the job using the nohup command

By submitting the job using the nohup command, by default all output from the job is redirected to a file named nohup.out unless an 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 to execute. If you want to close the task after getting the foreground, press Ctrl+c, but if you want to pause this task, you can press Ctrl+z. The task is placed in a paused state.

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

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

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

If the task you want to pause continues to execute later, you can use bg %jobnumber After this paused task continues to run in the background, it will become running

Someone may have noticed that the two task numbers 2 and 3 above are followed by ‘+’ and ‘-&rsquo ;, What does this mean for ‘+’? This ‘+’ is the task that is called by default in the background under the current window. It sounds a bit awkward to do an experiment:

The two tasks we saw above are all in a stopped state, normal. In the case of letting the task continue to run in the background is the bg %jobnumber command. I am doing the following operation. I directly enter the bg task 2 and it is activated. That is to say, in this window, the background will be activated with the task 2 of ‘+’ Now, it is 3 to bring ‘+’ can be repeated, now it should be understood; fg is the same as this situation, interested friends can try

[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=/d Ev/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 &

In addition, there are two commonly used ftp tools ncftpget and ncftpput, which can implement ftp upload and download in the background, so you can use these commands to upload and download files in the background.

The above is the usage of nohup under Linux. After using this command, the program will not be disconnected inexplicably, and it will always be executed in the background. I hope the content of this article will help you.

Copyright © Windows knowledge All Rights Reserved