I have a command that should take less than 1 minute to execute, but for some reason has an extremely long built-in timeout mechanism. I want some bash that does the following:
success = False
try(my_command)
while(!(success))
wait 1 min
if my command not finished
retry(my_command)
else
success = True
end while
How can I do this in Bash?
Look at the GNU
timeoutcommand. This kills the process if it has not completed in a given time; you’d simply wrap a loop around this to wait for thetimeoutto complete successfully, with delays between retries as appropriate, etc.If you must do it in pure
bash(which is not really feasible –bashuses lots of other commands), then you are in for a world of pain and frustration with signal handlers and all sorts of issues.The linked documentation does explain the command; I don’t really plan to repeat it all here. The synopsis is
timeout [options] duration command [arg]...; one of the options is-k duration. The-k durationsays “if the command does not die after the SIGTERM signal is sent at 60 seconds, send a SIGKILL signal at 70 seconds” (and the command should die then). There are a number of documented exit statuses; 124 indicates that the command timed out; 137 that it died after being sent the SIGKILL signal, and so on. You can’t tell if the command itself exits with one of the documented statuses.