Nohup commands for Linux

Source: Internet
Author: User
Tags exit in

When applying unix/linux, we usually want to let a program run in the background, so we will often use & at the end of the program to let the program run automatically. For example we want to run MySQL in the background:/usr/local/mysql/bin/mysqld_safe–user=mysql &. But there are many programs that do not want to mysqld the same, so we need to nohup command, how to use the Nohup command? This explains some of the uses of the Nohup command.

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.

After consulting the Red Flag Linux engineer, he also can not be solved, after executing on my terminal, the process that he starts is still running after shutting down the terminal.

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'm the one that closes the terminal 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.

This detail has not been noticed by me, so I recorded it here.

Attached: nohup Command Reference

Nohup command

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 2>&1 &

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

Use jobs to view tasks.

Use FG%n to close.

There are also two commonly used FTP tools Ncftpget and Ncftpput, can be implemented in the background FTP upload and download, so you can use these commands in the background to upload and download files.

Nohup command and its output file

Today, we deploy a WDT program on Linux and execute it on the SSH client./start-dishi.sh, start successfully, after closing the SSH client, the running program also terminates, how can we guarantee that the program can be executed after the launch of the SSH client? Find information on the Web and find that you need to use the Nohup command. Perfect solution: Nohup./start-dishi.sh >output 2>&1 &

The above command is now explained below.

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.

There are three common streams in the operating system:
0: Standard input stream stdin
1: Standard output stream stdout
2: Standard error flow stderr generally when we use > Console.txt, is actually 1>console.txt omitted usage;< console.txt, is actually 0 < console.txt omitted usage. Next Step:>nohup./start-dishi.sh >output 2>&1 & Explanation:
1. The command line with &, even if the terminal (terminal) is closed, or the computer crashes the program is still running (if you submit the program to the server); 2. 2>&1 means that the standard error (2) is redirected to the standard output (1), and the standard output is imported into the file output, so the result is standard error and standard output are imported into the file output. As to why the standard error needs to be redirected to the standard output, it boils down to a standard error without a buffer and stdout.   This causes the >output 2>output file output to be opened two times, and StdOut and STDERR will compete for coverage, which is certainly not what we want. That's why someone would write: Nohup./command.sh >output 2>output The cause of the error ====================================================== ============================ finally talk about the role of/dev/null file, this is a bottomless pit, anything can be directed to here, but cannot open. So generally very big Stdou and stderr when you don't care you can use stdout and stderr to direct to here >./command.sh >/dev/null 2>&1

Nohup commands for Linux

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.