How to run processes in linux background and processes in linux background
Linux runs a process directly. After the current command ends or the shell window is closed, the process ends.
How to run a process in the background
Method 1
Use the nohup command. The nohup command itself means that no hung up indicates that the shell will not be closed and the process will be closed.
Use nohup command & to run the command in the background, and run the command in the background through job-l.
If shell is directly closed, shell also closes the command in the background. To exit shell, you must run exit command before closing the command in the background.
Method 2
Run the script in the background. For example, run mongod -- dbpath = "abc" to start mongodb.
First, write a script test. sh, which can be
#! /Bin/sh
Mongod -- dbpath = "abc" Start mongodb &
# Here & is very important; otherwise, you cannot exit the current command.
Run test. sh directly. At this time, if mongodb has been started and runs to close the shell, it will not stop. The problem arises. Why is this happening? Someone on the Network explained it
Use test. THE sh operation ends immediately, and mongodb runs in the background with the & sign, test. sh runs the parent process in the current shell. (You can view the shell process number through echo $.), test. sh is running, but mongod is not running, test. sh will host mongod to the system init process. Through ps-ef | grep mongod, we can see that the parent process of mongod is 1.
Method 3
Use setsid to directly set the parent process of mongod as the init process
Setsid mongod -- dbpath = "abc" Start mongodb. Check whether the parent process of mongod is 1.
References: http://www.cnblogs.com/lwm-1988/archive/2011/08/20/2147299.html
How to run a process on the background in linux?
1. in Linux, if you want to run the process in the background, we usually add & to the end of the command. In fact, the command is put into a Job Queue:
$./Test. sh & [1] 17208 $ jobs-l [1] + 17208 Running./test. sh &
2. For commands that have been executed on the foreground, you can also run them in the background. First, press ctrl + z to pause the running processes, and then run the bg command to run the stopped jobs in the background:
$. /Test. sh [1] + Stopped. /test. sh $ bg % 1 [1] +. /test. sh & $ jobs-l [1] + 22794 Running. /test. sh &
3. However, for a process executed from the top to the backend, its parent process is still the shell process of the current terminal. Once the parent process exits, The hangup signal is sent to all sub-processes, the child process also exits after receiving the hangup. If we want to continue running the process when exiting the shell, we need to use nohup to ignore the hangup signal, or setsid to set the parent process as the init process (process number 1)
$ Echo $21734 $ nohup. /test. sh & [1] 29016 $ ps-ef | grep test 515 29710 21734 0 00:00:00 pts/12/bin/sh. /test. sh 515 29713 21734 0 00:00:00 pts/12 grep test
$ Setsid./test. sh & [1] 409 $ ps-ef | grep test 515 410 1 0? 00:00:00/bin/sh./test. sh 515 413 21734 0 00:00:00 pts/12 grep test
4. the above test demonstrates that the process is run in the background with nohup/setsid and is not affected by the current shell exit. So what should we do for processes that are already running in the background? You can use the disown command:
$. /Test. sh & [1] 2539 $ jobs-l [1] + 2539 Running. /test. sh & $ disown-h % 1 $ ps-ef | grep test 515 410 1 0? 00:00:00/bin/sh./test. sh 515 2542 21734 0 00:00:00 pts/12 grep test
5. Another method, even if the process is executed in a subshell, is similar to setsid. The method is simple. Enclose the command in parentheses:
$ (./Test. sh &) $ ps-ef | grep test 515 410 1 0? ... The remaining full text>
Linux Process running in the background
What do you want to ask?