Background execution commands:& and Nohup Command & and Close, view background tasks

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.


1. &

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  &
In this way, all standard output and error outputs are redirected to a file called Out.file.

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

2. Nohup command:

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: Nohup Command &

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:

Nohup Command > Myout.file 2>&1


Instance

nohup.sh

I=1while [$i-le 100000] doecho $i >> result.txti= ' expr $i + 1 ' done
1) Execute with SH nohup.sh, the current command line will hang,



2) using SH nohup.sh & execution, the job will be submitted to the background, releasing the current command line, while showing the process number , but if the current command window is closed, the entire job will stop;


Note: You can use Ps-ef | grep nohup.sh to view background job runs.


3) Use nohup sh nohup.sh > Log 2>1& &, also submit the job to the background, release the current window, and even if the current command window is closed, the job will still run.

Note: Use Nohup to execute commands, do not return the current process number, to track background jobs, you can use the Jobs command to view, or use Ps-ef | grep nohup.sh to view


3, CTRL + Z
You can place a command that is being executed in the foreground in the background and be in a paused state.


4. Ctrl + C

Terminates the foreground command.


5. 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.


6. BG
A command that pauses in the background changes to execute in the background. If there are multiple commands in the background, you can use BG%jobnumber to bring up the selected command.
, the foreground runs a script and then goes to the background to view background jobs



7, FG
The commands in the background are moved to the foreground to continue running. If there are multiple commands in the background, the selected command can be paged out with FG%jobnumber (which is the command number, not the process number).


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Background execution commands:& and Nohup Command & and Close, view background tasks

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.