Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Here is my take on this from 12 years ago after following the advice of a stack overflow post:

https://github.com/gentoo/genkernel/commit/a21728ae287e988a1...

With that (minus the gen_die() line unless you copy that helper function too), you can do:

  doSomething() {
      for i in {1..20}
      do
          sleep 1
          echo $i
      done
  }
  if ! call_func_timeout doSomething 10; then
      echo 'TIME OUT!'
      exit 1
  fi
Similarly to you, I only used shell builtins, plus the sleep command. The genkernel code is run by busybox ash, so the script had to be POSIX conformant. Note that both your script and my example script reimplementing your script with my code from 12 years ago, use {1..20}, which I believe is a bashism and is not POSIX conformant, but that is fine for your use case.

My innovation over the stack overflow post was to have the exit status return true when the timeout did not trigger and false when the time out did trigger, so that error handling could be done inline in the main script (even if that error handling is just printing a message and exiting). I felt that made code using this easy to read.



Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: