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--
In this issue, the editor will bring you about how to solve the Shell script execution timeout in Linux. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
An unusual situation in shell is that scripts sometimes time out. Generally speaking, when we encounter this kind of problem, we will simply and rudely use the following script to be an "overtime watchdog":
If this script is used with two variables, monitoring a little bit of code is OK, but its logic is still rough. For example, if a new process is started in the background when the script is executed, then the kill drops the new process, while the script that should have been stopped is still running unscrupulously in the background.
In order not to kill innocent people indiscriminately, in this case, you should use the timeout command. For specific usage, please use your own # man timeout, which is included in centos 6.
Suppose we want to ping www.baidu.com and ask "if there is no response for more than 5 seconds, stop the task." Then use the command: # timeout 5s ping www.baidu.com, as shown in the figure:
From 26 seconds to 31 seconds, it does achieve the effect of jumping out in 5 seconds.
The trial is over, so now let's evolve our previous MQ script. I wrote a MQ script in http://chenx1242.blog.51cto.com/10430133/1884415 before, but that MQ script was a little idealized. It forgot to add the two functions of "timeout monitoring" and "send email to remind the operation and maintenance staff if the restart fails". Here we will make up the above two shortcomings.
First, let's run it to see how long it takes to run the MQ watchdog script:
As you can see from the above, it takes about 13 seconds for the entire script to run, so we set the timeout to 20 seconds considering other factors, and the execution effect is as shown in the figure:
The return code is 0, so what is the return code that stops because of a timeout?
It can be seen that since the return code of timeout stop is 124 (the return code of ctrl+c manual exit is 130), the whole script is easy to write, as follows:
Then just execute the script directly in crontab.
One of the additional notes is that I used ping when testing the timeout command. In fact, this is not rigorous, because the ping of unix will be repeated by default, so # timeout 3s ping www.baidu.com will time out regardless of whether there is a network connection or not. The example here is to see the difference between # timeout 3s sleep 1 and # timeout 3s sleep 5.
Second, in the shell script, timeout followed by a function is invalid!
The above is the editor for you to share the Linux Shell script execution timeout how to solve, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are 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
© 2024 shulou.com SLNews company. All rights reserved.