Linux background execution commands: & and nohup

Linux background execution commands: & and nohup

Original  March 16, 2017 14:34:40
  • 32300

When we're working in a terminal or console, we probably don't want to run a job and take up the screen because there may be more important things to do, like reading emails. For a process that intensively accesses the disk, we prefer it to run during non-peak load times of the day (such as the early morning). To enable these processes to run in the background, that is, not on the terminal screen, there are several options available.


  • When a job is running in the foreground, the terminal is occupied by the job; you can add & after the command to run in the background. For example: sh test.sh & 
    suitable commands to run in the background are find, time-consuming sorting and some shell scripts. Be careful when running jobs in the background: commands that require user interaction should not be executed in the background, because then your machine will be waiting there. However, jobs running in the background will also output the results to the screen, interfering with your work. If a job running in the background produces a lot of output, it is best to redirect its output to a file using the following method:
command  >  out.file  2>&1  & 
  • 1

This way, all standard output and error output will be redirected to a file called out.file.

PS: When you successfully submit the process, a process number will be displayed, which can be used to monitor the process or kill it. (ps -ef | grep process ID or kill -9 process ID)

  • After nohup 
    uses the & command, the job is submitted to the background to run, and the current console is not occupied, but once the current console is closed (when the account is logged out), the job will stop running. The nohup command can continue to run the corresponding process after you log out of the account. nohup means not to hang up (no hang up). The general form of this command is:
nohup command &
  • 1

If you submit a job using the nohup command, by default all output from the job is redirected to a file named nohup.out, unless an output file is specified otherwise:

nohup command > myout.file 2>&1 &
  • 1

After using nohup, many people just ignore it. In fact, it is possible that when the current account is abnormally logged out or terminated, the command ends by itself. Therefore, after using the nohup command to run the command in the background, you need to use exit to log out of the current account normally, so as to ensure that the command runs in the background all the time.

  • ctrl + z 
    can put a command that is being executed in the foreground to the background and is in a paused state.

  • Ctrl+c 
    terminates the foreground command.

  • jobs 
    to see how many commands are currently running in the background. 
    The jobs -l option displays the PIDs of all jobs, and the status of jobs can be running, stopped, Terminated. But if the task is killed (kill), the shell removes the task's process ID from the list known to the current shell environment.

  • 2>&1 Analysis
command >out.file 2>&1 &
  • 1
  1. command>out.file redirects the output of the command to the out.file file, that is, the output content is not printed to the screen, but is output to the out.file file.
  2. 2>&1 is to redirect the standard error to the standard output, where the standard output has been redirected to the out.file file, that is, the standard error is also output to the out.file file. The last &, is to let the command execute in the background.
  3. Just imagine what 2>1 stands for, the combination of 2 and > represents error redirection, and 1 represents error redirection to a file 1 instead of standard output; replace it with 2>&1, and the combination of & and 1 represents standard output, It becomes an error redirected to standard output.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324471839&siteId=291194637