In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How to run nohup linux background output, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
Running a new weapon in the background: tmux about the use of jobs jobs encountered a problem nohup python flush.py &
Running like this, the nohup.out file is generated, but the content is always empty, not even if you try for a long time. A lot of time was wasted.
Because the output of the python is buffered, the out.log cannot see the output immediately. The-u parameter so that python does not enable buffering. Solve the problem that nohup python-u flush.py > flush.log 2 > & 1 & linux commands run in the background
There are two ways:
1. Command &: run in the background. If you turn off the terminal, it will stop running.
2. Nohup command &: run in the background. If you turn off the terminal, you will continue to run.
I. brief introduction
The biggest advantage that distinguishes Linux/Unix from Microsoft platform is that it is truly multi-user and multi-task. Therefore, there are also unique management ideas in task management.
We know that on Windows, we either keep a program running in the background as a service, or we stop the service. You can't let the program switch between foreground and background. Linux provides fg and bg commands that make it easy to schedule running tasks. Suppose you find that a program running in the foreground takes a long time, but needs to do something else, you can use Ctrl-Z, hang the program, and then you can see the system prompt:
[1] + Stopped / root/bin/rsync.sh
Then we can schedule the program to be executed in the background: (the number after bg is the job number)
# bg 1
[1] + / root/bin/rsync.sh &
Use the jobs command to view the running tasks:
# jobs
[1] + Running / root/bin/rsync.sh &
If you want to set it back to the foreground, you can use the
# fg 1
/ root/bin/rsync.sh
In this way, you can only wait for the task to be completed on the console.
& throw the instructions into the background to execute
[ctrl] + z pause the foreground task in the background
Jobs to check the working status of the background
Fg jobnumber brings the tasks in the background to the front desk for processing
Bg jobnumber puts tasks in the background to handle
Kill manages the tasks in the background
2. &
In Linux, when a job is run in the foreground, the terminal is occupied by the job; when the job is run in the background, it does not occupy the terminal. You can use the & command to put the job in the background. In effect, this puts commands into a job queue:
$. / test.sh &
[1] 17208$ jobs-l
[1] + 17208 Running. / test.sh &
Be careful when running jobs in the background: commands that require user interaction should not be executed in the background, because your machine will be waiting there. However, running the job in the background will also output the results to the screen, interfering with your work. If a job running in the background produces a large amount of output, it is best to redirect its output to a file using the following method:
Command > out.file 2 > & 1 &
In the above example, 2 > & 1 means that all standard output and error output will be redirected to a file called out.file. When you successfully submit a process, a process number is displayed that you can use to monitor the process or kill it.
Example: find the file named "httpd.conf" and redirect all standard output and error output to the file of find.dt:
# find / etc/httpd/-name "httpd.conf"-print > find.dt 2 > & 1 & [2] 7832
After successfully submitting the command, the system gives its process number 7832. For commands that have been executed in the foreground, you can also re-execute them in the background. First, press ctrl+z to pause the running process, and then use the bg command to put the stopped jobs in the background. For example, use ctrl+z to suspend the tesh.sh that is being executed in the foreground:
$. / test.sh [1] + Stopped. / test.sh$ bg% 1 [1] +. / test.sh & $jobs-l [1] + 22794 Running. / test.sh & $echo $21734$ nohup. / test.sh & [1] 29016$ ps-ef | grep test515 29710 21734 0 11:47 pts/12 00:00:00 / bin/sh. / test.sh515 29713 21734 0 11:47 pts/12 00 : 00:00 grep test$ setsid. / test.sh & [1] 409$ ps-ef | grep test515 410 10 11:49? 00:00:00 / bin/sh. / test.sh515 21734 11:49 pts/12 00:00:00 grep test
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 se
$screen-dmS screen_test$ screen-listThere is a screen on: 27963.screen_test (Detached) 1 Socket in / tmp/uscreens/S-jiangfeng.$ screen-r screen_test
Tsid will set the parent process to the init process (process number is 1)
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 test515 410 11:49? 00:00:00 / bin/sh. / test.sh515 2542 21734 0 11:52 pts/12 00:00:00 grep test$ (. / test.sh &) $ps-ef | grep test515 410 11 : 49? 00:00:00 / bin/sh. / test.sh515 12483 21734 0 11:59 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 ():
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:
After reading the above, have you mastered how to run the output of nohup linux in the background? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.