bash command timeout 20

Posted by Peter Burkholder Mon, 22 Jun 2009 18:51:00 GMT

At $WORK I’m needing to maintain a backup system wherein our backup server a) starts an SSH process to stop-and-dump our CMS service, then b) SCPs the dumpfile back to the backup servers for writing to tape. I’ve discovered that the stop-and-dump part of the process would hang for 24 hours* when the stop-and-dump perl script exited but the initiating OpenSSH sshd process would not exit, preventing the SCP process from going forward.

I’ve decided to put a command timeout on the SSH process, and here’s how it looks in bash:

# Inspired by:
# http://www.ultranetsolutions.com/BASH-terminate-command-after-timeout.html
cmd_timeout() {
   [ $# -eq 2 ] || die "cmd_timeout takes 2 arguments" 
   command=$1
   sleep_time=$2

   # run $command in background, sleep for our timeout then kill the process if it is running
   # $! has the pid of the backgrounded job
   $command &
   cmd_pid=$!

   # sleep for our timeout then kill the process if it is running
   ( sleep $sleep_time && kill $cmd_pid && echo "ERROR - killed $command due to timeout $sleep_time exceeded" ) &
   killer_pid=$!

   # 'wait' for cmd_pid to complete normally.  If it does before the timeout is reached, then
   # the status will be zero.  If the killer_pid terminates it, then it will have a non-zero 
   # exit status
   wait $cmd_pid &> /dev/null
   wait_status=$?

   if [ $wait_status -ne 0 ]; then 
      echo "WARNING - command, $command, unclean exit" 
   else
      # Normal exit, detach and clean up the useless killer_pid
      disown $killer_pid
      kill $killer_pid &> /dev/null
   fi

   return $wait_status
}

cmd_timeout "ssh myhost some_long-running_command" 
next_command
* but I ought to raise this on an openssh mailing list in case it’s a bug, but anyho…
Comments

Leave a comment

  1. very well done! 11 months later:
    thank you very much for this extremely well written solution. exactly what I've searched for. very clean implementation. kudos!
  2. one further suggestion... 11 months later:
    change if [ $wait_status -ne 0 ]; then to if [ $wait_status -eq 143 ]; then so the $killer_pid isn't left dangling if the command exits non-zero but isn't killed. otherwise, superb solution!
  3. Exchange about 1 year later:
    Great tutorial about bash command timeout , they are very easy to understand. Thanks!
  4. smart battery charger about 1 year later:
    Unfortunatley it is beginning to turn out to be the norm for individuals to invest all working day for the web instead of of seriously dwelling, what a shame.
  5. Dallas Home Security about 1 year later:
    This is beyond doubt a blog significant to follow. Youve dig up a great deal to say about this topic, and so much awareness. I believe that you recognize how to construct people pay attention to what you have to pronounce, particularly with a concern thats so vital. I am pleased to suggest this blog.
  6. krzyzowki about 1 year later:
    Great blog post, I have been waiting for that
  7. darmowe pozycjonowanie about 1 year later:
    Great blog post, I have been waiting for that
  8. darmowe krzyzowki about 1 year later:
    Great blog post, I have been waiting for that
  9. pozycjonowanie about 1 year later:
    Great blog post, I have been waiting for that
  10. linux about 1 year later:
    I am not sure where you are getting your information, but great topic
  11. VIN check over 2 years later:
    Well done, very important facts you have been providing.
  12. korean fashion over 2 years later:
    This Bash command always difficult seems to me! But after reading your article I understand this much!
  13. Diablo3 over 2 years later:
    hmm ,i'm not sure if this is what i'm looking for but anyway this is interresting and could be useful some day,thanks for taking time to write such cool stuff
  14. best sleep aid over 2 years later:
    Stuff like these desurves a twit and maybe a bookmark,your content is very good and i thank you for that
  15. The Diet Solution Program over 2 years later:
    Stuff like these desurves a twit and maybe a bookmark,your content is very good and i thank you for that
  16. Diablo 3 over 2 years later:
    What a great website, Il be sure to bookmark and come back later. Keep up the great work.
  17. carpet cleaning Boise over 2 years later:
    Think positively and you'll see progress. Pretty much interesting indeed. thanks!
  18. carpet cleaning Boise over 2 years later:
    Think positively and you'll see progress. Pretty much interesting indeed. thanks!
  19. domain hosting over 2 years later:
    I was extremely pleased to find this website.I wanted to thank you for this good understand I definitely enjoying every single small bit of it. I bookmarked it to my bookmark website listing and might be checking again soon.
  20. register website domain over 2 years later:
    Very helpful entry. A must read for anyone interested inthis sort of thing. I cannot wait to read more from you.
Comments