54

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
6
  • 1
    possible duplicate of Timeout a command in bash without unnecessary delay Commented May 8, 2013 at 8:33
  • I also tried that one but no success. Commented May 8, 2013 at 8:37
  • 1
    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. Commented May 8, 2013 at 8:38
  • 1
    See also stackoverflow.com/questions/526782/… Commented May 8, 2013 at 8:39
  • 2
    Also asked in superuser.com/questions/593006/… Commented May 8, 2013 at 14:19

5 Answers 5

103

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.

Sign up to request clarification or add additional context in comments.

7 Comments

is it available for all Linux distros?
for example: I want to run this command in script and terminal after 10 sec 'airodump-ng $wlan0'
@UmairRiaz timeout is part of the GNU coreutils package, it should be included in most Linux distributions
I tried with this command but no success. Can you give me example with this command? airodump-ng $wlan0
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.
|
32

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.

3 Comments

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.
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 }
That's neat, I like it !
2

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.

Comments

-1

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

Comments

-8

Use sleep and && operand:

sleep [time in seconds] && your-command

Example:

sleep 7 && echo 'Hello world!'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.