Run the task (Shell) in the background on UNIX, BG or nohup

Source: Internet
Author: User

Sometimes a task runs for a long time and needs to be run in the background. Otherwise, the task will be terminated after the console is launched. This article describes how to run the task in the background.

For example, for a shell script, longjobs. Sh takes a long time to run and information is printed to the console.

1. /longjobs. sh starts the task. At this time, information is constantly printed on the console. If you want to adjust the task to the background, you can use CTR + z (in bash (Linux's default shell, in other UNIX systems, you need to switch to bash through the bash command before starting the task, or switch KSh to KSh, not all shells support CTR + Z) to pause the task.

2. Run the command BG to redirect the task to the background. (However, due to the constant printing of information to the console, all tasks cannot work in the console at this time. If you exit the console, the task will also be terminated .)

[Therefore, when running a shell task in UNIX, it is best to redirect the information printed to the screen to a new file ,. /longjobs. sh> longjobs. out, 2, CTR + Z, 3, and BG are transferred to the background. Then, exit the console,

(Exit from Bash, return to the default shell, and exit the console. (If Putty is disabled, the task will continue to be executed.) But if Putty is disabled while it is still in bash, the task will be terminated]

Therefore, if you want to keep the task running in the background, you should not rely on BG for implementation, but should use nohup for implementation: (and it is feasible in all shells ).

The command is generally in the form of nohup command &
By default, all output of the job is redirected to a file named nohup. Out. You can also specify the output file as follows:

Nohup./longjobs. Sh> longjobs. out 2> & 1 &

Nohup means that when the terminal is disconnected (Hang up), the server is still running. Note that you need to press any key after running the nohup command to return to the shell prompt and then disconnect.

Here, I would like to add a difference between & and &.

& Is used to execute two commands at a time cmd1 & cmd2. If the previous cmd1 runs successfully, continue to run cmd2; otherwise, cmd2 does not run.

Date & time, both commands are executed.

Datq & time, both commands are not executed, because datq does not exist. (while datq; time will fail datq, and time will run normally)

In addition, | is opposite.

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.