Methods for background execution of processes

Source: Internet
Author: User

Background process:

Every time we execute the command in the Terminal window, the process will always occupy the terminal, go to the end of the process, during this period of time, our input in the terminal is useless. Also, when the terminal window is closed or the network connection fails, the terminal is opened again and the process is interrupted. This is because when the user logs off or the network disconnects, the SIGHUP signal is sent to the child process to which the session belongs, and the default process for this SIGHUP is to terminate the process that received the signal. So if the signal is not captured in the program, the session's process will exit when the terminal is closed.

To achieve the purpose of background execution, we are actually going to accomplish the following two goals:

    • Let the process out of the foreground terminal, so that we can continue to interact with the system through the terminal.

    • The process is no longer affected by terminal shutdown, that is, the system no longer sends a SIGHUP signal to the process after the terminal shuts down or even sends a signal program and does not exit.

The following commands are implemented around these two goals.

&

The first is the symbol & that we encounter most often, and attaching it behind a command allows the process to execute in the background without occupying the foreground interface. It actually opens a background job in the session, and we'll talk about the operation of the job later.

However, we will find that if the terminal is closed at this point, the process will still exit. This is because the,& symbol only allows the process to let the foreground terminal function, cannot let the process not be affected by the SIGHUP signal.

Nohup

Nohup should be another command we often use, which functions as if it were literally meant to make the process unaffected by the SIGHUP signal. However, when we use nohup PHP test.php we will find that the process will still occupy the foreground terminal, but even if the terminal is closed or disconnected, the program will be executed, in addition we will find that in the current folder is more than a file named Nohup.out.

This is because the function of nohup is simply to keep the process from being affected by the SIGHUP signal and will not yield the foreground terminal, and it will also establish nohup.out to store the output of the process in the command execution directory. If the process does not require output and you do not want Nohup to create the file, you can redirect the standard output and the standard error output.

We often put nohup and & together to a piece of use, execute commands as follows Nohup command >/dev/null 2>&1 & So, you can rest assured that the process to run the results.


Methods for background execution of processes

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.