Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command `timeout` does not work with Scala - why?

Setup: Ubuntu 12.04, 32 Bit; Scala 2.9.1; Java 1.6.0_24

Description:

While on the bash command line the command /usr/bin/timeout 10 scala -version works fine, it gets stuck when executed within a bash script.

Executing on command line (duration < 1 seconds):

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

The very same command put in a bash script got stuck:

testScript.sh:

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

Executing testScript.sh (duration 10 seconds):

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

Note: The problem does not appear with Java (which is used by Scala), it seems to be a Scala specific problem.

Question: Why does the timeout call in the script got stuck ?

How can I fix this / What would be a good workaround ?

like image 888
John Threepwood Avatar asked Aug 15 '12 10:08

John Threepwood


1 Answers

Try including the --foreground option. From man timeout:

--foreground

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.

like image 86
Shawn Chin Avatar answered Nov 13 '22 21:11

Shawn Chin