In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to close the terminal or realize the background operation of the program in Linux. It is very detailed and has a certain reference value. Friends who are interested must finish reading it.
Nohup
The most commonly used command is [nohup]. The name shows whether to suspend the run command, close the terminal or exit an account, and the process continues to run, also with the [&] symbol.
Usage: nohup Command [Arg... ] [&]
$nohup sh test.sh & # close the current terminal directly, and open another to view $ps-few | grep test.sh
As you can see from the above, the parent process of the test.sh we are running becomes [PID 1]., which is taken over by the linux main process.
Setsid
Nohup is ignoring the SIGHUP signal, is there any other way of thinking?
The setip command reopens the session, inherits the ID of the parent process group, and is out of the control of the parent process. As you can see from the following figure, the parent process of the test.sh script is already init 1. At this time, whether you close or exit the terminal, it has nothing to do with the background program.
$setsid sh test.sh
Another difference between setsid and nohup is that you don't need to use & background symbols.
Exit
Use the exit command to exit the terminal, our program will continue to run, this is why?
This is because when you exit the terminal using the exit command, you will not send a SIGHUP signal to the task to which the terminal belongs. It is controlled by the huponexit configuration item, and the default is off, which can be viewed using the shopt command.
[root@api ~] # shopt | grep huponexit huponexit off
Configure huponexit to on, use the exit command to exit again, and the task you belong to will exit with it.
[root@api ~] # shopt-s huponexit [root@api ~] # shopt | grep huponexit huponexit on
Trap
Trap commands linux built-in commands, actions to be taken when a specified signal is received, followed by the name of the signal to be processed. Common signals are as follows:
When a script is in the background and does not want the terminal to exit and close the program, we can add the trap command to the script to ignore the SIGHUP signal (suspend the process).
#! / bin/sh trap "" HUP while true;do date > > / root/test.txt sleep 1 done
The Linux screen command is used for multiple windows managers. It creates a terminal and lets the program run in it.
Screen sh test.sh &
As can be seen from the example, the parent process of the test script is the screen terminal process, while the parent process of the screen terminal, PID1, also completes the running of the background process.
The above is all the contents of the article "how to close the terminal or realize the background running of the program in Linux". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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
Alarm key value-Chinese interpretation-
© 2024 shulou.com SLNews company. All rights reserved.