0

I have an issue with executing application via /usr/bin/timeout in a bash script. In this specific case this is a simple python fabric script (fabric version 1.14) In order to install this version of fabric library run: pip install "fabric<2" There is no reproduction with new fabric 2.x.

Shell script causing issue:

[root@testhost:~ ] $ cat testNOK.sh
#!/bin/bash
timeout 10 ./test.py
echo "RETCODE=$?"
[root@testhost:~ ] $ ./testNOK.sh
[localhost] run: echo Hello!
RETCODE=124
[root@testhost:~ ] $

Similar script (without timeout) working fine

[root@testhost:~ ] $ cat testOK.sh
#!/bin/bash
./test.py
echo "RETCODE=$?"
[root@testhost:~ ] $ ./testOK.sh
[localhost] run: echo Hello!
[localhost] out: Hello!
[localhost] out:

RETCODE=0
[root@testhost:~ ] $

Manual execution from bash commandline with timeout working fine:

[root@testhost:~ ] $ timeout 10 ./test.py && echo "RETCODE=$?"
[localhost] run: echo Hello!
[localhost] out: Hello!
[localhost] out:

RETCODE=0
[root@testhost:~ ] $

Python2.7 test.py script

[root@testhost:~ ] $ cat test.py
#!/usr/bin/python
from fabric.api import run, settings

with settings(host_string='localhost', user='root', password='XXXXX'):
    run('echo Hello!')
[root@testhost:~ ] $

I have observed the same behavior on different Linux distributions.

Now the question is why application executed via timeout within bash script behaves in a different way and what would be the best solution to this issue?

4
  • Can you run env -i bash --norc to get a clean shell and verify that your command still works fine with manual execution? Aug 24, 2018 at 17:25
  • Yes, still working fine: # env -i bash --norc bash-4.4# timeout 10 ./test.py && echo "RETCODE=$?" [localhost] run: echo Hello! [localhost] out: Hello! [localhost] out: RETCODE=0 bash-4.4# Aug 24, 2018 at 17:37
  • Great. Now can you run echo 'timeout 10 ./test.py && echo "RETCODE=$?"' > myfile && env -i bash --norc myfile to verify that this exact command fails in a script in the same kind of shell? Aug 24, 2018 at 18:24
  • that one is failing as well as env -i bash --norc -c 'timeout 10 ./test.py && echo "RETCODE=$?"' which is failing too Aug 24, 2018 at 20:12

2 Answers 2

1

You need to invoke timeout with the --foreground option:

timeout --foreground ./test.py

This is only required if the timeout command is not executed from an interactive shell (that is, if it's executed from a script file).

Quoting from the timeout info page:

‘--foreground’
     Don’t create a separate background program group, so that the
     managed COMMAND can use the foreground TTY normally.  This is
     needed to support timing out commands not started directly from an
     interactive shell, in two situations.
       1. COMMAND is interactive and needs to read from the terminal for
          example
       2. the user wants to support sending signals directly to COMMAND
          from the terminal (like Ctrl-C for example)

What's actually going on in this case is that fabric (or something invokes) is calling tcsetattr to turn terminal echo off. I don't know why, but I suppose it has something to do with the process used to (not) collect the user password. (I just saw it in an strace; I made no attempt to find the call.) Attempting to change tty configuration from a background process will cause the process to block until it regains control of the tty, and that's what's happening.

It doesn't happen when timeout is not used because bash doesn't create a background program group. I suppose that fabric 2 avoids the call to tcsetattr.

You could probably also avoid the issue by avoiding password-based SSH authentication but I didn't try that.

You can also avoid the problem by redirecting stdin to /dev/null (either in the timeout command or in the invocation of the shell script.) If you don't need to forward stdin to the remote command (and you probably don't), that might also be useful.

4
  • Thank you for your in-depth analysis. Adding --foreground option indeed helps (application runs normally). Redirecting stdin to /dev/null timeout 10 python ./test.py 0>/dev/null causes application to crash with python IOError exception. Replacing password authentication with key authentication did not help. What is still not quite clear to me in the light of your analysis is the fact that the issue is observed only if app is invoked with the timeout tool from the bash script and from the command line is still working fine even with the use of timeout tool. Aug 25, 2018 at 6:55
  • oh, maybe when invoked from the commandline the foreground is just default and in the bash script there are different terminal settings. The foreground vs background and terminal settings is not exactly my world. Aug 25, 2018 at 7:05
  • @fakej: the redirect goes the other way: timeout 10 python ./test.py </dev/null.
    – rici
    Aug 25, 2018 at 7:30
  • oh, my bad, thank you for pointing that. I admit that it works well too! Aug 25, 2018 at 7:54
-1

You Can Use time out without using bash Just by using the time model in python

import time

time.sleep(5)
#change the 5 by the seconds that you need to set a timeout
3
  • I guess you meant signal.alarm(). time.sleep() will not terminate an application Aug 24, 2018 at 16:15
  • use better sys.exit() Aug 24, 2018 at 16:18
  • The purpose of coreutils timeout as well as signal.alarm() is to send signal to application after specific amount of time unless application terminates itself. time.sleep() and sys.exit() are not replacement for that! sys.exit() will terminate python script instantly without letting it run for specific amount of time. Aug 24, 2018 at 16:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.