How to run processes in the background in Linux

Source: Internet
Author: User
In Linux, how does one run processes in the background in Linux? if you want to run processes in the background, we can add & amp; after the command. In fact, in this way, the command is put into a job queue: [plain] $. /test. sh & amp; [1] 17208 $ jobs-l [1] +... in Linux, how does one make processes run in the background in Linux? if you want to run processes in the background, we usually add & after the command. In fact, in this way, the command is put into a job queue: [plain] $. /test. sh & [1] 17208 $ jobs-l [1] + 17208 Running. /test. sh & 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: [plain] $. /test. sh [1] + Stopped. /test. sh $ bg % 1 [1] +. /t Est. sh & $ jobs-l [1] + 22794 Running. /test. sh & however, for the process executed from the top to the background, 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) [plain] $ 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 [plain] $ 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 the above test demonstrates the use of nohup/setsid Plus & make the process run in the background, without the impact of the current shell exit. So what should we do for processes that are already running in the background? Run the disown command: [plain] $. /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 there is another way, even if the process is executed in a subshell, this is actually the same as setsid. The method is simple. enclose the command in parentheses (): [plain] $ (./test. sh &) $ ps-ef | grep test 515 410 1 0? 00:00:00/bin/sh./test. sh 515 12483 21734 0 00:00:00 pts/12 grep test
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.