The difference between running a Linux command background

Source: Internet
Author: User

A script is often run in a Linux shell using the background

So, what are the differences?

Commandcommand >/dev/nullcommand >/dev/null 2>&1command &command &>/dev/nullnohup Command & Amp;>/dev/null

Here is a brief discussion of the following, respectively

# take test.sh for example [[email protected] ~]# cat test.sh #!/bin/bash#while [' 1 ' = = ' 1 '];d oecho ' date ' +%y-%m-%d%h:%m:%s ' sleep 100done
Command

Direct execution, the command output will be displayed on the terminal interface, CTRL-C will terminate the command run

[[email protected] ~]# bash test.sh 2016-08-07 20:49:47^c[[email protected] ~]#
Command >/dev/null

Direct execution, the command standard output will be directed to the null device, that is, ignore the standard output 1, but error output 2 will show, in fact, this is equivalent to command 1 >/dev/null

[[email protected] ~]# cat test.sh#!/bin/bash#while [' 1 ' = = ' 1 '];d oecho ' Date "+%y-%m-%d%h:%m:%s" ' Llsleep 100done[[e Mail protected] ~]# bash test.sh >/dev/nulltest.sh:line 5:ll:command not found^c
Command >/dev/null 2>&1

Here the first and the above command will output 1 to a null device, 2 redirect to 1, then 2 will also guide the null device, so all outputs are sent to a null device

Command &

This must have been used by everyone in the background, but when we exit the terminal, the command terminates because our terminal is equivalent to a parent process, and once the parent process exits, it sends a hangup signal to all its child processes, and the child process exits when it receives it. I'm not going to give an example here. If we add nohup to the header, that is, using Nohup command &, then the parent process will not send hangup to this child process and will always run in the background

Command &>/dev/null and command >/dev/null 2>&1 difference

Online does not say much difference, it is talked about Ksh for the latter compatible better, that is, to meet the needs, so recommend the latter, of course, if the current platform to determine the use of the former seems more convenient, less, hit 4 characters

Background control
#  backstage commands can be viewed by Jobs [[email protected] ~]# jobs[1]-  running                  bash test.sh  &[2]+  Running                  bash test1.sh &#  adjust to front and rear to pass Bg %job_number fg %job_ Number to complete, such as job 1 to the front desk [[email protected] ~]# fg %1bash test.sh#  command line will continue to do so , Ctrl-z can let it stop running in the background [[email protected] ~]# fg %1bash test.sh^z[1]+  stopped                  bash  test.sh#  How to make it run in the background, because now the status is stopped,bg %1, you will find bg 1 also line [[EMAIL PROTECTED] ~]# BG  1[1]+ bash test.sh &[[email protected] ~]# jobs[1]-  running                  bash test.sh &[2]+   Running                  bash test1.sh &


This article is from the "Learning Path of Kai Learning" blog, please make sure to keep this source http://qixue.blog.51cto.com/7213178/1906781

The difference between running a Linux command background

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.