My original problem was to kill a process & its children when timeout. And I found GNU timeout
quite a good choice.
我最初的问题是在超时时杀死进程及其子进程。我发现GNU超时是个不错的选择。
However, in this testcase, things become weird:
然而,在这个测试中,事情变得奇怪了:
Assume that we have a test1.sh
like this:
假设我们有一个test1。上海是这样的:
#!/bin/sh
# test1.sh
output=`timeout 2 ./run.sh`
echo $output
and run.sh
like this:
和运行。上海是这样的:
#!/bin/sh
# run.sh
sleep 8s&
Intuitively we should expect test1.sh
exits instantly, since init
will take charge of that silly sleep
process and run.sh
will then exits.
直观上,我们应该期望test1。sh立即退出,因为init将负责这个愚蠢的睡眠过程并运行。sh将退出。
However:
然而:
sh-4.2$ time ./test1.sh
real 0m8.022s
user 0m0.013s
sys 0m0.003s
And if I create this test2.sh
:
如果我创建这个test2。sh:
#!/bin/sh
# test2.sh
timeout 2 ./run.sh
sh-4.2$ time ./test2.sh
real 0m0.014s
user 0m0.003s
sys 0m0.007s
So, obviously we hit something wrong during command substitution, but why?
显然,我们在命令替换过程中出错了,但是为什么呢?
1 个解决方案
#1
3
It may be the way you have in shell script --
这可能是你在shell脚本中的方式—
`timeout 2 ./run.sh`
-- you are using command substitution, so as long as the command has not finished execution, substitution cannot be done, because the output is not there...this may explain the output you are seeing.
——您正在使用命令替换,因此只要命令没有完成执行,就不能进行替换,因为输出不在那里……这也许可以解释您所看到的输出。
Try this to see a similar outcome....
试试这个....类似的结果
echo "hello `sleep 2 &`"
Another script of interest --
另一个有趣的脚本—
$ cat y.sh
echo "hi"
sleep 2 &
echo "bye"
sleep 2 &
Run using
运行使用
echo "hello `sh y.sh`"
$time sh y.sh
hi
bye
real 0m0.006s
user 0m0.000s
sys 0m0.004s
$time echo "hello `sh y.sh`"
hello hi
bye
real 0m2.008s
user 0m0.004s
sys 0m0.000s
This page explains more about the relationship between background process and file descriptor. Basicly:
这个页面解释了更多关于后台进程和文件描述符之间的关系。基本:
Background (better: forked) processes inherit the file descriptors, and Running a command in backticks means to collect its stdout until its stdout is closed
后台进程(更好的是:分叉)继承文件描述符,在回签中运行命令意味着收集它的stdout,直到它的stdout关闭
#1
3
It may be the way you have in shell script --
这可能是你在shell脚本中的方式—
`timeout 2 ./run.sh`
-- you are using command substitution, so as long as the command has not finished execution, substitution cannot be done, because the output is not there...this may explain the output you are seeing.
——您正在使用命令替换,因此只要命令没有完成执行,就不能进行替换,因为输出不在那里……这也许可以解释您所看到的输出。
Try this to see a similar outcome....
试试这个....类似的结果
echo "hello `sleep 2 &`"
Another script of interest --
另一个有趣的脚本—
$ cat y.sh
echo "hi"
sleep 2 &
echo "bye"
sleep 2 &
Run using
运行使用
echo "hello `sh y.sh`"
$time sh y.sh
hi
bye
real 0m0.006s
user 0m0.000s
sys 0m0.004s
$time echo "hello `sh y.sh`"
hello hi
bye
real 0m2.008s
user 0m0.004s
sys 0m0.000s
This page explains more about the relationship between background process and file descriptor. Basicly:
这个页面解释了更多关于后台进程和文件描述符之间的关系。基本:
Background (better: forked) processes inherit the file descriptors, and Running a command in backticks means to collect its stdout until its stdout is closed
后台进程(更好的是:分叉)继承文件描述符,在回签中运行命令意味着收集它的stdout,直到它的stdout关闭