Run commands in the background: & amp; and nohupcommand & amp; and disable and view background tasks.

Source: Internet
Author: User

Run commands in the background: & and nohupcommand & and disable and view background tasks

When we work on a terminal or console, we may not want to occupy the screen by running a job, because there may be more important things to do, such as reading emails. For intensive disk access processes, we want them to run during off-peak hours every day (for example, early morning ). To enable these processes to run in the background, that is, they do not run on the terminal screen, there are several options available.

1 ,&

When running a job on the foreground, the terminal is occupied by the job. You can add & implement background running after the command. Example: sh test. sh &

Commands suitable for running in the background include f I n d, time-consuming sorting, and some s h e l scripts. Be careful when running jobs in the background: do not execute commands that require user interaction in the background, because your machine will be silly there. However, running a job in the background will output the result to the screen, interfering with your work. If a job running in the background produces a large amount of output, you 'd better use the following method to redirect its output to a file:

 

command  >  out.file  2>&1  &
In this way, all the standard output and error output will be redirected to an out. file.

 

Note: After you successfully submit a process, a process number is displayed. You can use it to monitor the process or kill it. (Ps-ef | grep process number or kill-9 process number)
 

2. nohup command:

After you use the & command, the job is submitted to the background for running. The current console is not occupied, but once you turn off the current Console (when you exit the account), the job stops running. The nohup command can run the corresponding process after you exit the account. Nohup means no hang up ). The command is generally in the form of nohup command &

If you use the nohup command to submit a job, all the output of 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) run the command sh nohup. sh. The current command line will be hang,

 


 

2) use sh nohup. sh & execution: the job is submitted to the background, the current command line is released, and the process number is displayed. However, if the current command window is closed, the entire job is stopped;

Note: You can use ps-ef | grep nohup. sh to view the running status of background jobs.

3) Use nohup sh nohup. sh> log 2> 1 & to submit the job to the background and release the current window. Even if the current command window is closed, the job still runs.

Note: If you use nohup to execute the command, the current process number will not be returned. To track background jobs, you can use the jobs command to view them, or use ps-ef | grep nohup. sh to view them.

3. ctrl + z
You can put a command that is being executed in the foreground to the background and be paused.

4. Ctrl + c

Terminate the foreground command.

5. jobs
View the number of commands currently running in the background.
The jobs-l option displays the PID of all tasks. jobs can be in the running, stopped, and Terminated states. However, if the task is terminated (kill), shell deletes the process ID of the task from the List known in the Current shell environment.

6. bg
Change a command that is paused in the background to continue execution in the background. If there are multiple commands in the background, you can use bg % jobnumber to call up the selected command.
First, run a script on the foreground, and then go to the background to view the background job status.



7. fg
Move the commands in the background to the foreground to continue running. If there are multiple commands in the background, you can use fg % jobnumber (command number, not process number) to call up the selected command.

 

Related Article

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.