写脚本的时候,经常需要用到超时控制。看《shell专家编程》时看到一个好例:修改了一下,

1.超过timeout时间还没执行完,则kill进程,发邮件告警:

set-x
mailSend()
{
        mailContent="xxxx Web response time over 5 seconds"
        echo $mailContent | mail -s "xxxxxx Web TimeOut"xxxxx@xxx.cion
}
timeout()
{
        waitfor=3
        command=$*
        $command &
        commandpid=$!

( sleep $waitfor ; kill -9 $commandpid  >/dev/null2>&1&& mailSend )&

watchdog=$!
        sleeppid=$PPID
        wait $commandpid >/dev/null2>&1

kill $sleeppid >/dev/null2>&1
}

#测试的函数

test123()
{
        sleep 20
}

timeout test123


2.超过timeout时间还没执行完,只发邮件告警,程序正常执行:

mailSend()
{
        mailContent="xxxxe Web response time over 5 seconds,Please have a check !"
        echo $mailContent | mail -s "xxxxx WEB response time over 5 senconds" $mailTo
}
timeout()
{
        waitfor=6
        command=$*
        $command &
        commandpid=$!

( sleep $waitfor ;  mailSend )&

watchdog=$!
        sleeppid=$PPID
        wait $commandpid >/dev/null2>&1
        kill  -9 $watchdog >/dev/null2>&1
        kill $sleeppid >/dev/null2>&1
}

shell timeout的更多相关文章

  1. python免密远程执行shell

    使用paramiko库:https://github.com/paramiko/paramiko 简单封装SSH类 import paramiko class SSH: def __init__(se ...

  2. child_process小解

    js是一种单进程单线程的语言,但现行的cpu都是多核的,为了解决单进程单线程对多核使用不足的问题,child_process应运而生,理想情况下每个进程各自利用一个内核. 主要有四种方法来创建子进程, ...

  3. MIPCMS V3.1.0 远程写入配置文件Getshell过程分析(附批量getshell脚本)

      作者:i春秋作家--F0rmat 0×01 前言 今天翻了下CNVD,看到了一个MIPCMS的远程代码执行漏洞,然后就去官网下载了这个版本的源码研究了下.看下整体的结构,用的是thinkPHP的架 ...

  4. python 中的queue, deque

    python3 deque(双向队列) 创建双向队列 import collections d = collections.deque() append(往右边添加一个元素) import colle ...

  5. Python的并发并行[3] -> 进程[0] -> subprocess 模块

    subprocess 模块 0 模块描述 / Module Description From subprocess module: """Subprocesses wit ...

  6. zabbix ZBX_NOTSUPPORTED: Timeout while executing a shell script.

    有一个监控一直都是正常的,今天突然收到报警邮件,上服务器查看服务又是正常的,但是报警邮件还是没恢复 监控端进行脚本测试,发现是正常的 到监控端使用zabbix_get -s ip -p 端口  -k ...

  7. 解决zabbix“ZBX_NOTSUPPORTED: Timeout while executing a shell script”报错

    如题所示,在zabbix_server使用zabbix_get获取自定义“UserParameter”对应的脚本的数据时,出现了如题所示的报错信息 [root@nmp01 scripts]# /usr ...

  8. shell中timeout实现

    第一种 function timeout() { waitsec=$SLEEP_TIME ( $* ) & pid=$! ( sleep $waitsec && kill -H ...

  9. Linux shell脚本编程(一)

    Linux shell脚本编程: 守护进程,服务进程:启动?开机时自动启动: 交互式进程:shell应用程序 广义:GUI,CLI GUI: CLI: 词法分析:命令,选项,参数 内建命令: 外部命令 ...

随机推荐

  1. 解决UITabeleViewCell的分割线不能铺满问题

    -(void)viewDidLayoutSubviews { if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)] ...

  2. javascript——浅谈javascript模版(自定义)

    /** * Created by Administrator on 15-1-19. */ function functionUtil() { } functionUtil = { //某个DOM节点 ...

  3. 微信开发python+django两个月的成功经历,django是个好框架!

        时间:大三 上学期没有用微信内置浏览器而纯对话开发,坑了自己好一下. 下学期选错bottle框架,以为轻量好,谁知开发中什么都自己来很痛苦. 选对了框架django,终于在大三最后的个把月里写 ...

  4. 修改原代码定制bootstrap

    1.下载对应的Bootstarap和node.js 注:less文件夹中包含了bootstrap中所有样式组件的less源代码: dist保存编译后的css和js等文件 2.命令行输入npm inst ...

  5. Fatal error: Class 'ZipArchive' not found的解决办法

    今天在Linux底下编写导出EXCEL文件并显示输出时,抛出“ZipArchive library is not enabled” 的异常.而我在本地的windows下的代码则是运行正常的. 原因是: ...

  6. js时间戳转为日期格式

    转自:http://wyoojune.blog.163.com/blog/static/57093325201131193650725/ 这个在php+mssql(日期类型为datetime)+aja ...

  7. windows下实现uboot的tftp下载功能

    一.原理分析 带有uboot的开发板实际上充当的就是tftp客户端,而PC机扮演的角色就是tftp服务器端,而tftp下载功能实际上就是文件传输.tftp服务器可以建立在虚拟机linux下,也可以建立 ...

  8. spoj SORTBIT - Sorted bit squence

    Let's consider the 32 bit representation of all integers i from m up to n inclusive (m ≤ i ≤ n; m × ...

  9. 老oj曼哈顿最小生成树

    Description 平面坐标系xOy内,给定n个顶点V = (x , y).对于顶点u.v,u与v之间的距离d定义为|xu – xv| + |yu – yv| 你的任务就是求出这n个顶点的最小生成 ...

  10. MySQL存储过程实例

    一.创建MySQL数据库函数 TCC:无参数,查询fruit表中的所有数据 : TAA:两个参数,查询fruit总共有多少行:查询ids为某个值时水果表的数据 TDD:两个参数,查询ids不等于某个值 ...