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?
env -i bash --norcto get a clean shell and verify that your command still works fine with manual execution?echo 'timeout 10 ./test.py && echo "RETCODE=$?"' > myfile && env -i bash --norc myfileto verify that this exact command fails in a script in the same kind of shell?env -i bash --norc -c 'timeout 10 ./test.py && echo "RETCODE=$?"'which is failing too