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/02 Report--
What is the difference between running background tasks using nohup and screen in Linux? Many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can gain something.
Use SSH terminals (such as putty,xshell) to connect to the Linux server to run time-consuming tasks, which may take hours or even days to complete. At this time, the terminal is occupied, and we have to do other tasks, so we need to put this kind of time-consuming tasks to the background to run. You only need to record the log. Here are some common methods to achieve this requirement.
Command &
Enter command & to run at the terminal, and the abort command using Ctrl + C will fail because the program will continue to run if it is immune to the SIGINT1 signal running in the background. But if you turn off the terminal directly, the process will be aborted. It can be seen that the process running in the background will be aborted by the SIGHUP2 signal, but if you use the exit command to exit the terminal normally, the program will continue to run in the background and will not stop.
Nohup command
Standard output is appended to the nohup.out in the current directory by default, and if the nohup.out file in the current directory is not writable, the output is redirected to the $HOME/nohup.out file.
So, it's easy to think of combining these two features to run the command like this:
Nohup command > / home/user/myfile.log 2 > & 1 &
This will execute this task in the background until it is complete, and direct the program standard output (1) and standard error (2) to the / home/user/myfile.log file.
Screen tool
Screen can be understood as a session window management tool, for general use, we only need to use the following operations:
# use yum to install screenyum install screen# to create a session window named test screen-S test# temporary exit window Ctrl+a d (that is, hold down Ctrl Then press screen d) # to view the existing session window screen-ls# entry window screen-r testscreen-r process ID# close window exit# window switch Ctrl+a c: create window in the current screen session Ctrl+a w: window list Ctrl+a n: next window Ctrl+a p: previous window Ctrl+a 0-9: switch between window 0 and window 9
With the screen tool, we can create a window on the terminal, then run the commands we need, then leave temporarily, and then we can continue to do other things, of course, we can also use other terminals to connect to the server and use the screen-r command to connect to the window we need, and we will find that the program is running all the time, and exiting the terminal will not affect the programs running on the temporary terminals.
Test:
Let's write a test script test.sh:
#! / bin/shfor ((iTun1; I test.log 2 > & 1 & [1] 15037 [root@localhost test.cc] # ps-aux | grep test.shroot 15037 0.0 113180 1424 pts/0 S 16:10 0:00 / bin/sh. / test.shroot 15045 0.0 112712 992 pts/0 S + 16:10 grep-- color=auto test.shBash
You can see that 15037 processes are running
If the terminal is closed directly, logging stops, indicating that the process stops execution with the end of the terminal session.
However, after repeated tests, it is found that if you use the exit command to exit the terminal normally, and then connect to the terminal, the program running with & continues to run, that is, the command has the same effect as nohup, and the test environment I use here is CentOS7.6.
Use nohup & run
[root@localhost test.cc] # nohup. / test.sh > test.log 2 > & 1 & [1] 14349
Test results, directly close the terminal or use the exit command to exit the terminal, the program will continue to execute.
Run using screen
# create window screen-S test# to run commands. You don't need the following & here, but the program will occupy the window. / test.sh > test.log 2 > & 1 & # temporarily leave Ctrl + a d
At this time, you can directly close the terminal, and then reconnect, and find that the program is still running, if you want to find a window connection:
# existing window [root@localhost ~] # screen-lsThere is a screen on: 18048.test (Detached) 1 Socket in / var/run/screen/S-root.# entry window [root@localhost ~] # screen-r test
After entering, you will find that the window still remains in the same state as before, including the programs and commands entered before. When you enter the exit command in the window to exit the window, if you do not use & after the previous command, the program will stop, because compared to the test window, this is a foreground program, and the window exits the program naturally.
Because of the temporary departure function of screen, you can keep the foreground program running in the temporary departure window, which is equivalent to running in the background relative to the real terminal.
Program termination (interrupt) signal, issued when the user types an INTR character (usually Ctrl-C) to notify the foreground process group to terminate the process ↩︎
Issued when the terminal connection ends, usually shutting down the terminal and notifying each process group within the same session of ↩︎
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.