shell中timeout实现
第一种
function timeout() {
waitsec=$SLEEP_TIME
( $* ) & pid=$!
( sleep $waitsec && kill -HUP $pid ) 2>/dev/null & watchdog=$!
# if command finished
if wait $pid 2>/dev/null; then
pkill -HUP -P $watchdog
wait $watchdog
fi
# else: command interrupted
}
第二种
function timeout()
{
local time cmd pid
if echo "$1" | grep -Eq '^[0-9]+'; then
time=$1
shift && cmd="$@"
else
time=5
cmd="$@"
fi
$cmd &
pid=$!
while kill -0 $pid &>/dev/null; do
sleep 1
let time-=1
if [ "$time" = "0" ]; then
kill -9 $pid &>/dev/null
wait $pid &>/dev/null
fi
done
}
假设有一个测试脚本sleep.sh:
$ cat sleep.sh
echo "sleep $1 seconds"
sleep $1
echo "awake from sleep"
现在利用我们写的timeout函数来达到超时kill功能:
$ time sh timeout.sh 2 'sh sleep.sh 100'
sleep 100 seconds
real 0m2.005s
user 0m0.002s
sys 0m0.001s
看最终执行的时间,差不多就是2秒钟。
上面timeout函数实现的代码中,利用了两个技巧:
- kill -0 $pid:发送信号0给进程,可以检查进程是否存活,如果进程不存在或者没有权限,则返回错误,错误码为1;
- wait $pid &>/dev/null:等待某个进程退出返回,这样相对比较优雅,同时将错误重定向到黑洞,从而隐藏后台进程被kill的错误输出;
shell中timeout实现的更多相关文章
- shell中使用expect命令进行远程执行命令脚本
expect是用来实现自动交互功能的工具之一,使用expect-send来实现交互过程. 注意: 1.脚本的执行方法与bash shell不一样,比如:expect example.sh 2.向一个脚 ...
- linux shell 中的sleep命令
开始还以为是这样的语法: sleep(1), 后面发现是: linux shell 中的sleep命令 分类: LINUX 在有的shell(比如linux中的bash)中sleep还支持睡眠(分,小 ...
- shell 中的与、或表达式
今天总结一下linux shell中逻辑关机表达方式.逻辑与的表达: 1).if [ $xxx=a -a $xx=b ] 注:-a表示and的意思 2).if [ $xxx=a ] && ...
- shell简单用法笔记(shell中数值运算)二
shell中变量值,如果不手动指定类型,默认都是字符串类型: 例如: a= b= c=$a+#b echo $c 结果会输出:123+456 shell中,如果要进行数值运算,可以通过一下方法: 方法 ...
- shell中{}的妙用
shell中${}的妙用 1. 截断功能 ${file#*/}: 拿掉第一条/及其左边的字符串:dir1/dir2/dir3/my.file.txt ${file##*/}: 拿 ...
- Angular JS中$timeout的用法及其与window.setTimeout的区别
$timeout的用法 angular.js的$timeout指令对window.setTimeout做了一个封装,它的返回值是一个promise对象.当定义的时间到了以后,这个promise对象就会 ...
- shell中命令之间数据的传递
1.管道 "|" ls | cat -n > out.txt 2. 子shell 2.1 子shell 说明 在shell脚本中可以用()操作符可以定义一个子shell #/ ...
- shell中&&和||的使用方法
测试题: [ -z "" ] && echo 0 || echo 1 的结果是多少 看看这两个 && || 的用户 http://blog.csd ...
- 任督二脉之Shell中的正则表达式
VBird说学习Linux,掌握了Shell和正则就相当于打通了任督二脉,此后能力的成长才会突飞猛进. Shell的基础学习之前已经总结了一篇博客:http://www.cnblogs.com/jyz ...
随机推荐
- [转] Linux写时拷贝技术(copy-on-write)
PS:http://blog.csdn.net/zxh821112/article/details/8969541 进程间是相互独立的,其实完全可以看成A.B两个进程各自有一份单独的liba.so和l ...
- Could not reliably determine the server's fully qualified domain name
启动apache报错: [root@namenode1 ]# service httpd start Starting httpd: httpd: Could not reliably determi ...
- (转)Repeater在无数据记录时显示暂无数据
方法就是在FooterTemplate加个Label并根据repeater.Items.Count判断是否有记录.关键代码如下: <FooterTemplate> <asp: ...
- 使用CAEmitterLayer实现下雪效果
效果图: 代码部分: #import "ViewController.h" @interface ViewController () @end @implementation Vi ...
- 几种破解MySQL root密码的几种方法:
几种破解MySQL root密码的几种方法: 方法一 使用phpmyadmin,这是最简单的了,修改mysql库的user表,不过别忘了使用PASSWord函数. 方法二 使用mysqladmin,这 ...
- 如何在Eclipse中给main方法加参数
在main方法中有一个args参数,那么如何给args参数赋值呢? public class TestMain { public static void main(String[] args) { f ...
- 洛谷 P1515 旅行
P1515 旅行 题目描述 你要进行一个行程为7000KM的旅行,现在沿途有些汽车旅馆,为了安全起见,每天晚上都不开车,住在汽车旅馆,你手里现在已经有一个旅馆列表,用离起点的距离来标识,如下: 0, ...
- PHP防止SQL注入的方法
[一.在服务器端配置] 安全,PHP代码编写是一方面,PHP的配置更是非常关键. 我们php手手工安装的,php的默认配置文件在 /usr/local/apache2/conf/php.ini,我们最 ...
- php 产生不重复的随机数
$arr=array();//创建数组 while(count($arr)<10){ $a = mt_rand(1000,9999);//产生随机数 if(!in_array($a,$arr)) ...
- C程序设计语言练习题1-19
练习1-19 编写函数reverse(s),将字符串s中的字符顺序颠倒过来.使用该函数编写一个程序,每次颠倒一个输入行中的字符顺序.代码如下: #include <stdio.h> // ...