Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
4190076c
提交
4190076c
authored
6月 08, 2016
作者:
Jakub Sztandera
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add the time-out script
License: MIT Signed-off-by:
Jakub Sztandera
<
kubuxu@protonmail.ch
>
上级
da4e54c3
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
92 行增加
和
0 行删除
+92
-0
.gitignore
test/bin/.gitignore
+1
-0
time-out
test/bin/time-out
+91
-0
没有找到文件。
test/bin/.gitignore
浏览文件 @
4190076c
...
...
@@ -8,3 +8,4 @@
!checkflags
!continueyn
!verify-go-fmt.sh
!time-out
test/bin/time-out
0 → 100755
浏览文件 @
4190076c
#!/bin/bash
#
# The Bash shell script executes a command with a time-out.
# Upon time-out expiration SIGTERM (15) is sent to the process. If the signal
# is blocked, then the subsequent SIGKILL (9) terminates it.
#
# Based on the Bash documentation example.
# Hello Chet,
# please find attached a "little easier" :-) to comprehend
# time-out example. If you find it suitable, feel free to include
# anywhere: the very same logic as in the original examples/scripts, a
# little more transparent implementation to my taste.
#
# Dmitry V Golovashkin <Dmitry.Golovashkin@sas.com>
scriptName
=
"
${
0
##*/
}
"
declare
-i
DEFAULT_TIMEOUT
=
9
declare
-i
DEFAULT_INTERVAL
=
1
declare
-i
DEFAULT_DELAY
=
1
# Timeout.
declare
-i
timeout
=
DEFAULT_TIMEOUT
# Interval between checks if the process is still alive.
declare
-i
interval
=
DEFAULT_INTERVAL
# Delay between posting the SIGTERM signal and destroying the process by SIGKILL.
declare
-i
delay
=
DEFAULT_DELAY
function
printUsage
()
{
cat
<<
EOF
Synopsis
$scriptName
[-t timeout] [-i interval] [-d delay] command
Execute a command with a time-out.
Upon time-out expiration SIGTERM (15) is sent to the process. If SIGTERM
signal is blocked, then the subsequent SIGKILL (9) terminates it.
-t timeout
Number of seconds to wait for command completion.
Default value:
$DEFAULT_TIMEOUT
seconds.
-i interval
Interval between checks if the process is still alive.
Positive integer, default value:
$DEFAULT_INTERVAL
seconds.
-d delay
Delay between posting the SIGTERM signal and destroying the
process by SIGKILL. Default value:
$DEFAULT_DELAY
seconds.
As of today, Bash does not support floating point arithmetic (sleep does),
therefore all delay/time values must be integers.
EOF
}
# Options.
while
getopts
":t:i:d:"
option
;
do
case
"
$option
"
in
t
)
timeout
=
$OPTARG
;;
i
)
interval
=
$OPTARG
;;
d
)
delay
=
$OPTARG
;;
*
)
printUsage
;
exit
1
;;
esac
done
shift
$((
OPTIND
-
1
))
# $# should be at least 1 (the command to execute), however it may be strictly
# greater than 1 if the command itself has options.
if
((
$#
==
0
||
interval <
=
0
))
;
then
printUsage
exit
1
fi
# kill -0 pid Exit code indicates if a signal may be sent to $pid process.
(
((
t
=
timeout
))
while
((
t
>
0
))
;
do
sleep
$interval
kill
-0
$$
||
exit
0
((
t -
=
interval
))
done
# Be nice, post SIGTERM first.
# The 'exit 0' below will be executed if any preceeding command fails.
kill
-s
SIGTERM
$$
&&
kill
-0
$$
||
exit
0
sleep
$delay
kill
-s
SIGKILL
$$
)
2> /dev/null &
exec
"
$@
"
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论