What about shell script execution timeouts?

Source: Internet
Author: User

In the shell there will be a less common situation, that is, the script sometimes has a time-out phenomenon. Generally speaking, we will use the following script as a "timeout watchdog":

#!/bin/bash./$1 & #这里设定后台运行的第一个参数pid = $! #最后一个运行的process的PID值sleep $ && kill-9 $pid & #如果超过了 The value of $, it'll kill the PID.


This script with two variables to use, monitoring a little bit of code is OK, but its logic is still relatively rough, such as when the script executes, there is a new process in the background to start, then kill is the new process, And the script should be stopped in the background unscrupulous running.


In order not to kill innocent people, so encountered this situation, it is necessary to use the Timeout command, the specific usage please self- #man timeout, this command in CentOS 6 is self-brought.


Let's say we want to ping www.baidu.com and ask, "if it's not responding for more than 5 seconds, stop the task." So that's the command:#timeout 5s Ping www.baidu.com, the effect

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M01/9C/C4/wKioL1l1veSDGXk0AABEEUKVgSU170.png "title=" 1.png "alt=" Wkiol1l1vesdgxk0aabeeukvgsu170.png "/>

From 26 seconds to 31 seconds, it does reach the effect of 5 seconds.


Kind is over, so now we're going to evolve the MQ script we had before, in http://chenx1242.blog.51cto.com/10430133/1884415 I wrote an MQ script, but the MQ script was a little idealistic, Forgot to add "time-out monitoring" and "Restart failed to send email alert operators" Two features, here we put the above two short board.


First, let's run a look at the time this MQ watchdog script needs to run:

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M01/9C/C6/wKiom1l10GmgzuPZAACB1mHT1eU721.png "title=" 1.png "alt=" Wkiom1l10gmgzupzaacb1mht1eu721.png "/>


It takes about 13 seconds for the entire script to run from above, so we consider other factors setting the timeout to 20 seconds, performing the effect

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/9C/C6/wKiom1l10vuibFHcAABu6EDeCCg218.png "title=" 1.png "alt=" Wkiom1l10vuibfhcaabu6edeccg218.png "/>


The return code is 0, then look at what if the return code stopped because of the timeout?

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/9C/C6/wKiom1l1007QG3j7AAAoeR9gSME594.png "title=" 1.png "alt=" Wkiom1l1007qg3j7aaaoer9gsme594.png "/>


Visible because the return code of the timeout stop is 124 (the return code of CTRL + C manual exit is 130), then the entire script is well written, as follows:

#!/bin/bashmailcontent= "Detect rmsmq Script execution timeout, please log on to the server to view" timeout 20s python checkrmsmq.pyif [[$ = = 124]]; Then echo "Watchdog did not execute within the normal time frame. "Echo $mailContent | Mail-s "rmsmq watchdog script execution timed out!         "E-mail address @163.comelse; The echo watchdog script is executed within the normal time frame. "Fi


Then execute the script directly in the crontab.


One of the supplemental instructions, when testing the timeout command in the text, I used a ping, in fact, this is not rigorous, because the UNIX ping default will be infinitely repeated, so #timeout 3s ping www.baidu.com The network connection will time out regardless of whether or not. The best example here is to look at the difference between #timeout 3s sleep 1 and #timeout 3s sleep 5 .


Second, in the shell script, if the function after timeout is invalid!




Finally, if you feel this article is helpful for your promotion, please do not hesitate to sponsor the hand, brush the following QR code, sponsorship I continue to write more blog posts!

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M00/9C/C8/wKioL1l16m3BMYDKAACPHEqd55Q687.jpg "title=" Webwxgetmsgimg.jpg "alt=" Wkiol1l16m3bmydkaacpheqd55q687.jpg "/>

This article is from "Life is waiting for Gordo" blog, please make sure to keep this source http://chenx1242.blog.51cto.com/10430133/1950570

What about shell script execution timeouts?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.