50

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.

67

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 ;-))

| improve this answer | |
  • 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 '11 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 – user1527227 Jun 19 '14 at 16:42
  • 2
    What a sexy command. Thanks for pointing it out, wasn't aware of it. – Bruno Bieri Mar 16 '17 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. – Vladimir Iashin Apr 23 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.