Linux background execution script file, nohup

Source: Internet
Author: User

Look at the operation and maintenance personnel to execute Nohup command, put the program in the background execution, very tall, on the study, this command.

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.

Example:

 A 2>&1 &

Note:2>&1 is a whole, there is no space in the middle, indicating that the standard output and error output are transmitted to the specified file.

Here is an explanation:

ls xxx 1>out.txt 2>&1
nohup/mnt/nand3/h2000g >/Dev/NULL2>&1 &
For & 1 More accurate should be the file descriptor 1, and 1 is generally representative of Stdout_fileno, in fact, this operation is a dup2 (2) call. He standard output to All_result, and then copy the standard output to the file descriptor 2 (stderr_ Fileno), the consequence is that the file descriptor 1 and 2 point to the same file table entry, or the wrong output is merged. where 0 means keyboard input 1 means that the screen output 2 indicates an error output. REDIRECT standard error to standard output, then throw to/DEV/NULLGo below. In layman's words, all standard output and standard errors are thrown into the trash.
Command >out.file 2>&1 &
Command >out.file redirects the command output to the Out.file file, which means that the output is not printed to the screen, but is output to the Out.file file. 2>&1 is redirecting standard errors to standard output, where the standard output has been redirected to the Out.file file, and the standard error is output to the Out.file file. The last & is to have the command execute in the background.

Imagine what 2>1 stands for, the combination of 2 and > represents error redirection, while 1 represents the error redirection to a file 1, not the standard output;
Replacing 2>&1,& with 1 represents the standard output, which becomes an error redirect to the standard output.

You can use
LS 2>1 test, will not report the error of No 2 file, but will output an empty file 1;
ls xxx 2>1 test, no xxx This file error output to 1;
ls xxx 2>&1 test, will not generate 1 of this file, but the error ran to the standard output;
ls xxx >out.txt 2>&1, actually can be replaced with ls xxx 1>out.txt 2>&1; redirect symbol > default is 1, error and output are uploaded to OUT.txt.
why 2>&1 to write in the back?
Command > File 2>&1
The first is command > file to redirect the standard output to file, 2>&1 is the standard error copy of the standard output behavior, which is also redirected to file, the end result is that the standard output and errors are redirected to file.
Command 2>&1 >file
The 2>&1 standard error copies the behavior of the standard output, but at this point the standard output is still in the terminal. The output is redirected to file after >file, but the standard error remains at the terminal.

With Strace you can see:
1. Command > File 2>&1
The key system call sequences that implement redirection in this command are:
Open (file) = = 3
Dup2 (3,1)
Dup2 (ON)

2. Command 2>&1 >file
The key system call sequences that implement redirection in this command are:
Dup2 (ON)
Open (file) = = 3
Dup2 (3,1)

Why use/dev/null 2>&1 this kind of writing. This command means that the standard output and the error output are all redirected to/dev/null, which is all the information that is generated is discarded. I'll take a look at it for you,command > File 2>fileAndcommand > File 2>&1There are different places.
First ~command > File 2>fileThe standard output information generated by the command, and the error output information is sent to file.command > File 2>fileIn this way, stdout and stderr are sent directly to file, file will be opened two times, so stdout and stderr will cover each other, so write quite use FD1 and FD2 two simultaneously to seize file of the pipeline.
andcommand >file 2>&1This command sends stdout directly to file, StdErr inherits the FD1 pipeline, and is sent to file, where file is opened only once, and only a pipe FD1 is used, which includes the contents of stdout and stderr.
From IO efficiency, the efficiency of the previous command is less efficient than the following command, so when writing a shell script, we usecommand > File 2>&1This way of writing.

Reference URL: http://itersky.blog.163.com/blog/static/197177205201262355827645/

Linux background execution script file, nohup

Related Article

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.