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.
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 ;-))
-
1Perfect, 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
-
1Pretty 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 -
2What 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 sendSIGTERM
. You can useh
andd
for hours and days. – Vladimir Iashin Apr 23 '20 at 8:02