mediatribe.net -- Drupal and Web Development

Notice: this post was last updated 4 years 2 weeks ago so it might be outdated. Please be cautious before implementing any of suggestions herein.

Limiting the execution time of a command line on Mac OS X

Unfortunately the "timeout" command does not exist on mac OS X.

(sleep 2;echo 'done')  & sleep 1; kill $! 2> /dev/null || :

will not display "done" because the first command executed in more than 1 second.

(sleep 2;echo 'done')  & sleep 3; kill $! 2> /dev/null || :

will not display "done" because the first command executed in less than 3 second.

Walking through this code:

sleep 2;echo 'done': replace this with whatever command you want to keep in check.

sleep 3: replace 3 with whatever max number of seconds you want.

kill $!: kills your prcess after a set number of seconds

2> /dev/null: ignores errors thrown by kill when your command has executed in less than the set number of seconds.

|| :: ignores the "fail" exit code when your code executes in less than the set number of seconds. This is useful if you are running this command in Jenkins for example and you don't want jenkins to set your build to fail when things go well.

I was in fact looking for

I was in fact looking for this and I am fortunate enough to find it here on this site. Thanks

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <h3>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.