bash command timeout 14

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. ac condenser 10 months later:
    im just kinda confuse because awhile ago i was googling these words "free blogger sites" and when i clicked one of the results i was redirected here, im reading a lot of thing about "typo" i just wanna ask if this typo thing is a free blogger platform?
  2. rolex replica 10 months later:
    good post
  3. bridesmaid dresses 10 months later:
    Thank you for sharing, It can help us a lot.
  4. rolex replica watches 11 months later:
    good post
  5. 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!
  6. 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!
  7. wedding dresses 11 months later:
    I recently came across your blog and have been reading along. I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.
  8. Marriage Counseling      about 1 year later:
    Marriage Counseling
  9. Holiday Tour Guide about 1 year later:
    You have a point. Very insightful. A nice different perspective.
  10. Online Electronic Products about 1 year later:
    Thanks for this article. It's just what I was searching for. I am always interested in this subject. Will bookmark it.
  11. dcsadasd about 1 year later:
    ell Replica watches Through
  12. dcsadasd about 1 year later:
    ell Replica watches Through
  13. 3G Mobile Phones about 1 year later:
    A complete resource for knowing all about 3G in India, Networks, plans, tariff, review etc.
  14. dcsadasd about 1 year later:
    ng stage shows with Elmo, Big mens prada Bird, Gordon, and Abby Cadabby. replica belts louis vuitton</a
Comments

leave url/email »)