48

I am making a bash script for my use. How can I run a command for certain time, like 20 seconds and terminate command? I tried a lot of solutions but nothing works, I also tried timeout command with no success. Please give me some solution for this.

For example: I want to run this command in script and terminal after 10 sec

some command
Vertexwahn
  • 7,709
  • 6
  • 64
  • 90
Umair Riaz
  • 489
  • 1
  • 4
  • 5
  • 1
    possible duplicate of [Timeout a command in bash without unnecessary delay](http://stackoverflow.com/questions/687948/timeout-a-command-in-bash-without-unnecessary-delay) – Mat May 08 '13 at 08:33
  • I also tried that one but no success. – Umair Riaz May 08 '13 at 08:37
  • There's fourteen answers to that question, including external links. "no success" doesn't mean anything. Please [edit] your question to describe **exactly** what you tried and precisely what doesn't work about it. – Mat May 08 '13 at 08:38
  • 1
    See also http://stackoverflow.com/questions/526782/how-do-i-limit-the-running-time-of-a-bash-script/526815#526815 – paxdiablo May 08 '13 at 08:39
  • I added example by editing question – Umair Riaz May 08 '13 at 08:40
  • 2
    Also asked in http://superuser.com/questions/593006/bash-run-command-for-certain-time – fedorqui May 08 '13 at 14:19

5 Answers5

92

Here are some bash scripts and a program called timelimit which may solve your problem. Kill process after it's been allowed to run for some time

EDIT: I think I found a better solution. Try using the timeout program. From the man page: "timeout - run a command with a time limit". For example:

timeout 5s sleep 10s

It basically runs your command and after the specified duration it will kill it.

Community
  • 1
  • 1
Robert Gomez
  • 1,269
  • 9
  • 9
  • 1
    is it available for all Linux distros? – Umair Riaz May 08 '13 at 08:58
  • for example: I want to run this command in script and terminal after 10 sec 'airodump-ng $wlan0' – Umair Riaz May 08 '13 at 10:06
  • 2
    @UmairRiaz `timeout` is part of the GNU coreutils package, it should be included in most Linux distributions – toro2k May 08 '13 at 10:52
  • I tried with this command but no success. Can you give me example with this command? airodump-ng $wlan0 – Umair Riaz May 08 '13 at 10:58
  • 1
    I'm pretty sure you would do `timeout 10s airodump-ng $wlan0`. If that's not working then maybe there is something special about your command. – Robert Gomez May 08 '13 at 11:20
  • after this command, I got blank screen when terminal reach to this command. – Umair Riaz May 08 '13 at 17:37
  • Did you disable your wireless interface before you tried running `airodump-ng`? Have you looked at the [documentation](http://www.aircrack-ng.org/doku.php?id=airodump-ng) for the program? By the way, I hope you're not breaking into people's Wifi networks... – Robert Gomez May 08 '13 at 17:55
26

On systems that do not provide timeout command, you can use the following:

your-cmd & sleep 30 ; kill $!

That will run potentially long running your-cmd with timeout of 30 seconds.

If your-cmd does not finish within 30 seconds, it will be sent TERM signal.

mato
  • 593
  • 4
  • 11
  • 1
    This is not ideal, because when your-cmd only takes a second, the command still waits for 30 seconds and ending up with an error, because there is no PID to kill anymore. – Martin Melka Aug 15 '17 at 08:08
  • Unfortunately the whole issue is more complex. Right now I use the following (and it is not ideal either): timelimit() # $1 - timeout in seconds { local t="$1" shift ( "$@" ) & local pid=$! ( sleep "$t" && killtree $pid ) >/dev/null 2>&1 & local wpid=$! wait $pid 2>/dev/null local rc=$? killtree $wpid >/dev/null 2>&1 return $rc } – mato Aug 24 '17 at 12:05
  • That's neat, I like it ! – Tom Raganowicz Feb 08 '19 at 08:42
1

I suggest 2 options:

  • On systems where you have the timeout command, you can use it according to man timeout.
  • On systems without it (e.g. CentOS 5), you can use the script supplied with bash /usr/share/doc/bash-3.2/scripts/timeout. Use it according to its usage information.
keypress
  • 759
  • 9
  • 12
0

Are you looking to schedule the execution of the script? Then cron is your friend.

Matthew Graves
  • 3,226
  • 1
  • 17
  • 20
-8

Use sleep and && operand:

sleep [time in seconds] && your-command

Example:

sleep 7 && echo 'Hello world!'