In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to make the process run in the background under the Linux environment". In the daily operation, I believe that many people have doubts about how to make the process run in the background under the Linux environment. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to make the process run in the background under the Linux environment". Next, please follow the editor to study!
one。 Nohup / setsid / &
Usage scenario: if there is only a temporary command that needs to be run for a long time, what is the easiest way to ensure that it runs steadily in the background?
There are two ways to solve this problem: either let the process ignore the HUP signal, or let the process run in a new session and become a child process that does not belong to this terminal.
Solution:
1.nohup
Simply add nohup to the command to be processed, and standard output and standard error are redirected to the nohup.out file by default. Usually we can add "&" at the end to run the command in the background at the same time, or we can use > filename 2 > & 1 to change the default redirect file name.
[root@pvcent107 ~] # nohup ping www.ibm.com & [1] 3059 nohup: appending output to `nohup.out' [root@pvcent107 ~] # ps-ef | grep 3059 root 3059 984 0 21:06 pts/3 00:00:00 ping www.ibm.com root 3067 984 0 21:06 pts/3 00:00:00 grep 3059 [root@pvcent107 ~] #
2. Setsid
Setsid is also very convenient to use, and you just need to add setsid before the command to be processed.
[root@pvcent107 ~] # setsid ping www.ibm.com [root@pvcent107 ~] # ps-ef | grep www.ibm.com root 31094 10 07:28? 00:00:00 ping www.ibm.com root 31102 29217 0 07:29 pts/4 00:00:00 grep www.ibm.com [root@pvcent107 ~] #
In the above example, our process ID (PID) is 31094, and its parent ID (PPID) is 1 (that is, init process ID), which is not the process ID of the current terminal. Compare this example with the parent ID in the nohup example.
3. &
Including one or more names in the "()" allows these commands to run in the child shell. When we put "&" in the "()" as well, we will find that the submitted job is not in the job list, that is, it cannot be viewed through jobs.
[root@pvcent107 ~] # (ping www.ibm.com &) [root@pvcent107 ~] # ps-ef | grep www.ibm.com root 16270 10 14:13 pts/4 00:00:00 ping www.ibm.com root 16278 15362 0 14:13 pts/4 00:00:00 grep www.ibm.com [root@pvcent107 ~] #
The parent ID (PPID) of the newly committed process is 1 (the PID of the init process), which is not the process ID of the current terminal. Therefore, it does not belong to the child process of the current terminal, so it will not be affected by the HUP signal of the current terminal.
two。 Disown
Usage scenario: if you add nohup or setsid before the command, you can avoid the influence of the HUP signal. But if we have submitted the command without any processing, how can we remedy it so that it can avoid the influence of the HUP signal?
Solution: it is too late to add nohup or setsid, and this problem can only be solved through job scheduling and disown
Use disown-h jobspec to make a job ignore the HUP signal.
Use disown-ah to make all jobs ignore HUP signals.
Use disown-rh to make running jobs ignore HUP signals.
After using disown, the target job will be removed from the job list, and we will no longer be able to use jobs to view it, but we can still find it with ps-ef.
Disown example 1 (if you have already used "&" to run the command in the background when submitting the command, you can use "disown" directly)
[root@pvcent107 build] # cp-r testLargeFile largeFile & [1] 4825 [root@pvcent107 build] # jobs [1] + Running cp-I-r testLargeFile largeFile & [root@pvcent107 build] # disown-h% 1 [root@pvcent107 build] # ps-ef | grep largeFile root 4825 968 1 09:46 pts/4 00:00:00 cp-I-r testLargeFile largeFile root 4853 968 0 09:46 pts/4 00:00:00 grep largeFile [root@pvcent107 build] # logout
Disown example 2 (if you submit a command without using "&" to run it in the background, you can use CTRL-z and "bg" to put it in the background, and then use "disown")
[root@pvcent107 build] # cp-r testLargeFile largeFile2 [1] + Stopped cp-I-r testLargeFile largeFile2 [root@pvcent107 build] # bg% 1 [1] + cp-I-r testLargeFile largeFile2 & [root@pvcent107 build] # jobs [1] + Running cp-I-r testLargeFile largeFile2 & [root@pvcent107 build] # disown-h% 1 [root@pvcent107 build] # ps-ef | grep largeFile2 root 5790 5577 1 10:04 pts / 3 00:00:00 cp-I-r testLargeFile largeFile2 root 5824 5577 0 10:05 pts/3 00:00:00 grep largeFile2 [root@pvcent107 build] #
Three: screen
Usage scenario: we already know how to protect processes from HUP signals, but if there are a large number of such commands that need to be run in a stable background, how to avoid doing this for every command?
Solution: the most convenient method at this time is screen. To put it simply, screen provides a terminal emulator for ANSI/VT100, which enables it to run multiple full-screen pseudo-terminals under a real terminal. Screen has many parameters and has powerful functions.
Use screen-dmS (sessionName) to establish a session in disconnected mode (and specify its session name).
Use screen-list to list all sessions.
Use screen-r (sessionName) to reconnect the specified session.
Use the shortcut CTRL-a d to temporarily disconnect the current session.
Screen instance
[root@pvcent107] # screen-dmS Urumchi [root@pvcent107 ~] # screen-list There is a screen on: 12842.Urumchi (Detached) 1 Socket in / tmp/screens/S-root. [root@pvcent107] # screen-r Urumchi
When we connect to the screen session with "- r", we can do whatever we want in this pseudo-terminal, no longer have to worry about the impact of HUP signals on our progress, or add "nohup" or "setsid" to every command.
1. The process tree of the new process when not using screen
[root@pvcent107] # ping www.google.com & [1] 9499 [root@pvcent107] # pstree-H 9499 init ─┬─ Xvnc acpid ├─ atd ├─ 2* [sendmail] ├─ sshd ─┬─ sshd ─── bash ─── pstree │ └─ sshd ─── bash ─── ping
When we are not using screen, the bash we are in is a child process of sshd. When ssh is disconnected, the HUP signal will naturally affect all the child processes below it (including our newly established ping process).
two。 The process tree of the new process after using screen
[root@pvcent107 ~] # screen-r Urumchi [root@pvcent107 ~] # ping www.ibm.com & [1] 9488 [root@pvcent107 ~] # pstree-H 9488 init ─┬─ Xvnc ├─ acpid ├─ atd screen ─── bash ─── ping ├─ 2 * [sendmail]
This is different with screen, where bash is a child of screen and screen is a child of init (PID is 1). Then when the ssh is disconnected, the HUP signal naturally does not affect the child processes under the screen.
At this point, the study on "how to make the process run in the background in the Linux environment" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.