56

I want know how I can run a command for a specified time say, one minute and if it doesn't complete execution then I should be able to stop it.

1 Answer 1

73

Use timeout:

NAME
       timeout - run a command with a time limit

SYNOPSIS
       timeout [OPTION] DURATION COMMAND [ARG]...
       timeout [OPTION]

(Just in case, if you don't have this command or if you need to be compatible with very very old shells and have several other utterly specific requirements… have a look at this this question ;-))

4
  • 1
    Perfect, thanks for linking to that question. I didn't have timeout on my system but I do now. Just on a side note, is timeout bundled with the majority of linux distributions?
    – nikhil
    Oct 24, 2011 at 4:28
  • 1
    Pretty cool. you can do something like this: for i in `seq 0 3`; do timeout 2 ethtool -p eth$i; done; to blink LED's on NIC's Jun 19, 2014 at 16:42
  • 2
    What a sexy command. Thanks for pointing it out, wasn't aware of it. Mar 16, 2017 at 14:16
  • Example: timeout 5s vi will run the editor for 5 seconds and will send SIGTERM. You can use h and d for hours and days. Apr 23, 2020 at 8:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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