Linux redirection and Nohup methods __linux

Source: Internet
Author: User
Tags stdin
turn from: http://linchunai1212.blog.163.com/blog/static/35112143201172743917829/Let's talk about Linux redirection:
0, 1, and 2 represent standard input, standard output, and standard error information output, which can be used to specify standard inputs or outputs that require redirection.
In general use, the default is the standard output, both 1. When we need special use, we can use other labels. For example, the error message for a program is exported to the log file:./program 2>log. The standard output is still on the screen, but the error message is exported to the log file.
In addition, redirects between 0,1,2 can also be implemented. 2>&1: redirect error information to standard output.
Linux also has a special file/dev/null, it is like a bottomless pit, all redirected to its information will disappear without a trace. This is useful when we can redirect output to/dev/null when we do not need to echo all the information about the program.

If you want normal output and error messages to be displayed, redirect both standard output and standard error to/dev/null, for example:

# ls 1>/dev/null 2>/dev/null

Another approach is to redirect errors to standard output, and then redirect to/dev/null, for example:

# ls >/dev/null 2>&1

Note: The order here cannot be changed, otherwise the desired effect is not achieved, the standard output is redirected to/dev/null first, then the standard error is redirected to standard output, and the standard error is redirected to/dev/null because the standard output has been redirected to/dev/null. So it's all quiet:-)

Because of the use of nohup, the output will be automatically written to the Nohup.out file, if the file is very large, nohup.out will continue to increase, this is what we do not want to see, so we can use/dev/null to solve this problem.

(1) Discard standard output, output error to log file

Nohup./program >/dev/null 2>log &

(2) If the error message is not wanted:

Nohup./program >/dev/null 2>&1 &

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

About redirection, reference article: http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=484163

1. Basic Concepts

A, I/O redirection is usually related to FD, the shell is usually 10 fd, that is, 0~9; (fd:file descripter, file descriptor)
b, commonly used FD has 3, for: 0 (stdin, standard input), 1 (stdout, standard output), 2 (stderr, standard error Output), the default and keyboard, monitor, monitor related;
C, the use of < to change the read Data channel (stdin), from the designated file read;
D, use > To change the sent out data channel (stdout, stderr), so that the output to the designated file;
E, 0 is the default value of <, so < is the same as 0<;,> is the same as 1>;
F, in IO Redirect, stdout and stderr pipeline will be ready before the data from stdin read;
G, Pipeline "|" (Pipe line): The stdout of the previous command receives the stdin of the next command;
H, tee command is without affecting the original I/O, the stdout copies of a copy to the file;
I, Bash (Ksh) procedure to execute a command: Parse command-variable Evaluation-command substitution (' and $ ())-redirect-wildcard expansion-Determine path-execute command;
J, () put command group Sub-shell to execute, also known as nested Sub-shell, it has a very important feature is: Inherit the parent shell standard input, output, and error plus a NY other Open file descriptors.
K, EXEC command: Often used to replace the current shell and restart a shell, in other words, did not start the child shell. Any existing environment will be purged when this command is used. When exec operates on a file descriptor, it is only then that EXEC does not overwrite your current shell environment.

2. Basic IO

cmd > File redirect stdout to file
cmd >> file redirect stdout to file (append)
CMD 1> fiel redirect stdout to file
cmd > File 2>&1 redirect stdout and stderr to file
cmd 2> file redirect stderr to file
cmd 2>> file redirect stderr to file (append)
cmd >> file 2>&1 redirect stderr and stderr to file
The cmd < file >file2 cmd command takes the file file as stdin to file2 files as stdout
Cat <>file Open File in read-write mode
cmd < file cmd command with the file file as stdin
CMD << delimiter here document, read from stdin until you encounter the delimiter delimiter

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.