In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to make the process run in the background in Linux". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
In Linux, if you want the process to run in the background, you can usually add & to the command. In fact, this puts the command into a job queue:
$. / test.sh &
[1] 17208
$jobs-l
[1] + 17208 Running. / test.sh &
For commands that have been executed in the foreground, you can also re-execute them in the background, first pause the running process by pressing ctrl+z, and then use the bg command to put the stopped jobs to run in the background:
$. / test.sh
[1] + Stopped. / test.sh
$bg 1
[1] +. / test.sh &
$jobs-l
[1] + 22794 Running. / test.sh &
However, if the process is executed from the top to the background, the parent process is still the process of the current terminal shell, and once the parent process exits, it will send hangup signals to all child processes, and the child process will exit after receiving the hangup. If we want to continue running the process when we exit shell, we need to use nohup to ignore the hangup signal, or setsid will set the parent process to the init process (process number is 1)
$echo $$
21734
$nohup. / test.sh &
[1] 29016
$ps-ef | grep test
515 29710 21734 0 11:47 pts/12 00:00:00 / bin/sh. / test.sh
515 29713 21734 0 11:47 pts/12 00:00:00 grep test
$setsid. / test.sh &
[1] 409
$ps-ef | grep test
515 410 10 11:49? 00:00:00 / bin/sh. / test.sh
515 413 21734 0 11:49 pts/12 00:00:00 grep test
The above experiment demonstrated the use of nohup/setsid plus & to make the process run in the background without being affected by the current shell exit. So what about 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 10 11:49? 00:00:00 / bin/sh. / test.sh
515 2542 21734 0 11:52 pts/12 00:00:00 grep test
There is another way, even if the process is executed in a subshell, which is similar to setsid. The method is simple, enclosing the command in parentheses ():
$(. / test.sh &)
$ps-ef | grep test
515 410 10 11:49? 00:00:00 / bin/sh. / test.sh
515 12483 21734 0 11:59 pts/12 00:00:00 grep test
Note: the test environment of this article is Red Hat Enterprise Linux AS release 4 (Nahant Update 5) and shell is / bin/bash. Different OS and shell commands may be different. For example, AIX's ksh does not have disown, but you can use nohup-p PID to get the same effect as disown.
A more powerful way is to use screen, first create a virtual terminal in disconnected mode, and then reconnect the virtual terminal with the-r option. Any command executed in it can achieve the effect of nohup, which is convenient when multiple commands need to be executed continuously in the background:
$screen-dmS screen_test
$screen-list
There is a screen on:
27963.screen_test (Detached)
1 Socket in / tmp/uscreens/S-jiangfeng.
$screen-r screen_test
This is the end of "how to make processes run in the background in Linux". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.