How Linux runs the process in the background

Source: Internet
Author: User

Linux runs a process directly, and the process ends after the current command finishes, or when the shell window is closed.

How to run a process in the background

Method 1

Using the Nohup command, the nohup command itself means no hung up means that the shell is not closed and the process is closed.

Use Nohup Command & to enable commands to run in the background, and job-l to see what is running in the background.

The place where the pit is compared is if you close the Shell,shell, it will also close the background command, exit the shell must first through the command exit and then close in order to run in the background

Method 2

Use scripts to run in the background, for example I want to run Mongod--dbpath= "abc" to start MongoDB.

First, write a script test.sh, the content can be

#!/bin/sh

Mongod--dbpath= "abc" Start MongoDB &

#这里 & is important, you cannot exit the current command.

Then run the test.sh directly. This time MongoDB has been started and run off the shell will not stop, the problem comes, why so, someone on the network explained as

Use test.sh run will immediately end, MongoDB through the & to run in the background, test.sh run the parent process is the current shell, (view the shell process number can be viewed through the echo $$), Test.sh run the end, but Mongod will not run the end, TEST.SH will be responsible for hosting the Mongod to the system init process, through Ps-ef|grep Mongod can see the mongod of the parent process is 1.

Method 3

Set Mongod's parent process directly to the INIT process via Setsid

Setsid mongod--dbpath= "abc" starts MongoDB, viewing the Mongod process discovers that its parent process is 1.

Reference: http://www.cnblogs.com/lwm-1988/archive/2011/08/20/2147299.html

How Linux runs the process in the background

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.