6

设置: Ubuntu 12.04,32 位;斯卡拉 2.9.1; Java 1.6.0_24

描述:

虽然在 bash 命令行上该命令/usr/bin/timeout 10 scala -version工作正常,但在 bash 脚本中执行时会卡住。

在命令行上执行(持续时间< 1秒):

user@ubuntu:~$ /usr/bin/timeout 10 scala -version
Scala code runner version 2.9.1 -- Copyright 2002-2011, LAMP/EPFL
user@ubuntu:~$ echo $?
1

放在 bash 脚本中的相同命令卡住了:

testScript.sh

#!/bin/bash
/usr/bin/timeout 10 scala -version
echo "finished with $?"

执行testScript.sh(持续时间 10 秒):

user@ubuntu:~/scripts$ ./testScript.sh
Scala code runner version 2.9.1 -- Copyright 2002-2011, LAMP/EPFL
finished with 124
user@ubuntu:~/scripts$ 

注意:Java(Scala 使用)不会出现此问题,它似乎是 Scala 特有的问题。

问题:为什么timeout脚本中的调用卡住了?

我该如何解决这个问题/什么是一个好的解决方法?

4

1 回答 1

9

尝试包括该--foreground选项。来自man timeout

- 前景

When not running timeout directly from a shell prompt, allow COMMAND to read from the TTY and receive TTY signals. In this mode, children of COMMAND will not be timed out.

Using the following test script:

#!/bin/bash
/usr/bin/timeout --foreground 10 scala -version
echo "finished with $?"

It appears to work fine.

$ ./test.sh 
Scala code runner version 2.9.1 -- Copyright 2002-2011, LAMP/EPFL
finished with 1

Without --foreground the script hangs as you've described.

于 2012-08-15T11:57:47.377 回答