Linux background execution commands:& and nohup

Source: Internet
Author: User

When we work in a terminal or console, we may not want to occupy the screen because of running a job, because there may be more important things to do, such as reading e-mails. For dense access to the disk process, we would prefer it to run during the non-load peak time of day (e.g., early morning). Several options are available to enable these processes to run in the background, that is, to not run on the terminal screen.

    • &
      When you run a job in the foreground, the terminal is occupied by the job, and you can add & to run the background after the command. Example: SH test.sh &
      Commands that are suitable for running in the background have f i n D, time-consuming sorting, and some S-H-e l scripts. Be careful when running a job in the background: commands that require user interaction are not executed in the background, because your machine will be there to wait. However, a job running in the background will output the results to the screen, interfering with your work. If a job that runs in the background produces a lot of output, it's a good idea to redirect its output to a file using the following method:
command  >  out.file  2>&1 & 
    • 1

In this way, all standard output and error outputs are redirected to a file called Out.file.

PS: When you successfully submit a process, a process number is displayed, which can be used to monitor the process, or to kill it. (Ps-ef | grep process number or kill-9 process number)
    • Nohup
      After using the & command, the job is submitted to run in the background, the current console is not occupied, but once the current console is turned off (when exiting the account), the job will stop running. The Nohup command can continue to run the appropriate process after you exit the account. Nohup is the meaning of not hanging (no hang up). The general form of the command is:
command &
    • 1

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:

command > myout.file 2>&1 &
    • 1
After using the nohup, a lot of people do not care, in fact, it is possible that the current account is not normal exit or end of the time, the command or the end of their own. So after running the command in the background with the Nohup command, you need to exit the current account normally with exit so that the command runs in the background.
    • CTRL + Z
      You can place a command that is being executed in the foreground in the background and be in a paused state.

    • CTRL + C
      Terminates the foreground command.

    • Jobs
      See how many commands are currently running in the background.
      The JOBS-L option shows the Pid,jobs status of all tasks can be running, stopped, Terminated. However, if the task is terminated (kill), the shell removes the process identity of the task from the list known to the current shell environment.

    • 2>&1 parsing
command >out.file 2>&1 &
    • 1
    1. Command>out.file is to redirect the command output to the Out.file file, that is, the output is not printed to the screen, but is output to the Out.file file.
    2. 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.
    3. Imagine what 2>1 stands for, 2 and > are represented by error redirection, while 1 represents an error redirect to a file 1 instead of standard output, and a combination of 2>&1,& and 1 represents the standard output, which becomes an error redirect to standard output.

Linux background execution commands:& and nohup

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.