详解:nohup ./start.sh> myout.file 2>&1 & ---- 2016-06-02

nohup ./start.sh> myout.file 2>&1 &

The above command is commonly used by everyone, and executes the command of a command file in the background. But, what does ./ mean, what does > mean,

You may not know what 2>&1 and  & mean. Here are the related introductions found:

1. The difference between nohup and &:

nohup means running in the background, & means returning directly to the shell interface, without waiting for the end of the command you entered, so using it like this is the (nohup) command to run in the background, even if ssh is interrupted, it can run, (&) allows you to enter After this line of commands, continue to appear '>' for you to enter other commands

A single &, is equivalent to directly start receiving the next command without waiting. When you close the terminal, it will stop running
nohup command & background operation, and it will continue to run when you close the terminal.

 

2. The meaning of ./, the difference from sh:

sh xxx
uses the sh shell (sh generally refers to the system default shell, such as bash, ksh, Csh, etc. are possible) to interpret and run the xxx script. The xxx file does not have to have executable attributes (chmod +x)

./xxx xxx must have executable attributes. If xxx is a text file (script) at this time, then interpret and execute the command specified in the first line of xxx xxx, if not specified in the xxx file, it will be interpreted and executed according to /bin/sh by default. xxx needs to use the method
#!/path/to/mmm    in the first line
to indicate that the mmm command should be used to interpret and execute itself.
For example, if it is a bash script, it is #!/bin/bash
perl script, #!/usr/bin/perl
python script, #!/usr/bin/python

 

3. The meaning of 2>&1:

In fact, to understand the meaning of 2>&1, you should first know that there are three standard input and output in linux, namely STDIN, STDOUT, STDERR, and the corresponding numbers are 0, 1, and 2. STDIN is the standard input, which reads information from the keyboard by default; STDOUT is the standard output, which outputs the output to the terminal by default, that is, the display or something; STDERR is the standard error message, which is also displayed on the terminal by default. Since both STDOUT and STDERR will be displayed on the terminal by default, in order to distinguish the information of the two, there are definitions of numbers 0, 1, and 2, with 1 for STDOUT and 2 for STDERR.

Here is an example: Take the example in rhce as an example.

(1), create an ordinary user test in the system;

(2) Log in as an ordinary user, or log in as root su -test to switch to the ordinary user test;

(3) Execute the find /etc -name passwd command. By default, the execution result (STDOUT) and error message (STDERR) of the command will be output to the terminal display.

(4), experience the role of numbers 1 and 2, find /etc -name passwd >find.out 2>find.err, where STDOUT and STDERR will be stored in find.out and find.err respectively, this command also Can be written in the following three forms, we can understand the meaning of the number.

find /etc -name passwd 1>find.out 2>find.err

find /etc -name passwd 2>find.err >find.out

find /etc -name passwd 2>find.err 1>find.out

My personal understanding is that the execution result of the find /etc -name passwd command outputs the correct output (STDOUT) and is received by 1, and the wrong information (STDERR) is received by 2.

(5) If you want to display all the output and error information, you can use & to represent all the information of 1 and 2, for example:

find /etc -name passwd &>find.all

(6), sometimes you want to redirect the wrong information to the output, that is, redirect the result of 2 to 1. There is a way of thinking like "2>1". If you follow the above writing method, the system will default to the wrong message. The message (STDERR) 2 is redirected to a file named 1, not the expected (STDOUT). Therefore, you need to add & to distinguish. There is a usage like 2>&1, for example:

find /etc -name passwd 2>&1 |less

(7), sometimes you can also see such usage:

find /etc -name passwd &2>&1 |less

This can be broken down into

find /etc -name passwd & means that the previous command is executed in the background.

2>&1 |less means redirect the error message to standard output and use less for pagination.

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326616221&siteId=291194637