(reprinted) Nohup command to let Linux programs execute in the background forever

Source: Internet
Author: User
Tags exit in

Unix/linux generally want to let a program run in the background, many use & at the end of the program to let the program run automatically. our own program is only a normal program, generally this program even use & end, if the terminal is closed, then the program will be closed.

In order to be able to run in the background, we need to use the Nohup command, such as we have a start.sh need to run in the background, and want to be able to run in the background, then use Nohup:

nohup /root/start.sh &

Prompt after carriage return in shell:

[~]$ 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 in this step there is a problem, when the terminal is closed, the process will be automatically closed, see Nohup.out can be seen in the shutdown Terminal Instant service automatically shut down. (Jobs View Task)

The second time I showed it, I found out that I was in a different detail than when he operated the terminal: he was prompted by the Shell to return to the Shell Input Command window by pressing the keyboard key on the terminal, and then exit the terminal by entering exit in the shell Nohup And I am the one that closes the terminal immediately after the Nohup executes successfully after the Close Program button. So this time will be broken the corresponding session of the command, resulting in nohup corresponding process is notified need to shutdown together.

Attached:nohup Command Reference

Purpose: To run the command without hanging off.

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

Description: The nohup command runs commands specified by the command parameter and any related ARG parameters, ignoring all hang-up (SIGHUP) signals. Use the Nohup command to run a program in the background after logging off. To run the Nohup command in the background, add & (the symbol representing "and") to the end of the command.

The output is appended to the Nohup.out file in the current directory, regardless of whether the output of the Nohup command is redirected to the terminal. If the nohup.out file for the current directory is not writable, the output is redirected to the $HOME/nohup.out file. If no file can be created or opened for appending, then the command specified by the commands parameter is not callable. If the standard error is a terminal, then all output of the specified command to the standard error is redirected to the same file descriptor as the standard output.

Exit Status: The command returns the following exit values:

126 you can find but not invoke commands specified by the command parameter.

The 127 nohup command has an error or cannot find the command specified by the commands parameter.

Otherwise, the exit status of the Nohup command is the command parameter that specifies the exit state of the commands.

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 exit the account, you can use the Nohup command. This command can continue to run the process after you exit the account/close the terminal. Nohup is the meaning of not hanging (n ohang up).

The general form of the command is: Nohup Command &

To submit a job using the Nohup command

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

Nohup Command > Myout.file &

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

Task Close: kill Command

    • Scenario 1: View the job number (assuming num) through the jobs command, then execute: Kill%num
    • Scenario 2: View the Job's process number (PID, assuming PID) via the PS command, then execute: Kill PID

Termination of the foreground process: Ctrl + C

After shutting down the terminal, jobs in the other terminal cannot see the program running in the background, at this time can only use PS (Process view command): such as ps-u username

all processes for a user: Killall-u username    

The source of this article: http://blog.csdn.net/cyq1984/article/details/7531297

(reprinted) Nohup command to let Linux programs execute in the background forever

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.