Linux background Running program, input and output redirect __linux

Source: Internet
Author: User
Linux background Run program, input and output redirection

Linux background running programs using the, such as Python main.py, the background run main.py, that is, CTRL + C after the program is still running in the background, PS aux|grep "python main.py" can be viewed.

But when your Linux terminal shuts down, the program is not running, it accepts the signal of your terminal disconnect and shuts down the program. So how to make the system ignore the terminal disconnect signal, the terminal shutdown after the program is still running.

It's easy to add a nohup to the command to ignore the terminal disconnect signal, and if you want to stop the program, you can use the KILL command only. The default output to the Nohup.out file when using Nohup is to use input-output redirection in Linux if you want to enter other files.

Input-output redirection is supported in the Linux Shell environment, represented by the symbol "<" and ">". 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, such as 2>a.txt to output error messages to file A.txt.

In the meantime, redirects can be achieved between these three standard input outputs, such as redirecting error messages to standard output, and can be implemented using 2>&1.

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:-)

In addition +:

Make >& Make_log

The meaning is to redirect both standard output and error output, make>out only the standard output is redirected!!!!!

>log means redirecting standard output to file log

>& log indicates that both standard output and error output are directed to the file log, equivalent to >log 2>&1


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.