Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have often a situation in Linux shell scripts that if a specific command takes too long to run e.g. due to a network problem the shell script should abort running this command.

What best practices there exist to run timeoutting commands in a shell script? E.g. run commands through some kind of monitoring command with certain maximum running time given as seconds?

what I am looking for is to have something like "timeout -s 300 'this command line'"

Cron solutions are out of the question and not accetable answers.

share|improve this question
1  
There is a package called timeout, debian based sudo apt-get install timeout. The issue there is that the command only has the allotted time to run, so if you are ssh'ing and do timeout 300 ssh example@example.com, the task is killed after 300 seconds. That being said, there was a similar question on StackOverflow with a script implementation here that might do it. –  nerdwaller Nov 26 '12 at 16:16
    
Hi @nerdwaller: Is it possible to promote your comment to an answer? –  Mikko Ohtamaa Dec 7 '12 at 6:54
    
There you go, it worked for you? –  nerdwaller Dec 7 '12 at 14:09

1 Answer 1

up vote 1 down vote accepted

There is a package called timeout, debian based sudo apt-get install timeout. The issue there is that the command only has the allotted time to run, so if you are ssh'ing and do timeout 300 ssh example@example.com, the task is killed after 300 seconds. That being said, there was a similar question on StackOverflow with a script implementation here that might do it.

Though I tried this yesterday again and it was fine, so it .any depend on what command follows it.

share|improve this answer
    
Kiitoksia! :) Also available as standalone downhere bashcookbook.com/bashinfo/source/bash-4.0/examples/scripts/… (works on OSX too) and here is some of my notes about using this command opensourcehacker.com/2012/12/07/… –  Mikko Ohtamaa Dec 7 '12 at 19:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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