7

I tried to kill a process if it exceeds more than a few seconds.

The following works just fine when I run it in the terminal.

timeout 2 sleep 5

But when I have a script -

#!/bin/bash
timeout 2 sleep 5

it says

timeout: command not found

Why so? What is the workaround?

--EDIT--

On executing type timeout, it says -

timeout is a shell function

11
  • What do you get if you run type timeout in shell? Commented Mar 28, 2014 at 13:57
  • @user657592 which bash version you are using ? and also check /usr/bin/timeout exists or not Commented Mar 28, 2014 at 14:08
  • Is that the only timeout you get if you run type -a timeout? In general you will need to export the function if you have it implemented somewhere to use it in a script, but like @RahulPatil mentioned timeout is usually just a c function unless you have some other implementations. Commented Mar 28, 2014 at 14:09
  • @BroSlow - The same. timeout is a shell function Commented Mar 28, 2014 at 14:11
  • @user657592 Add export -f timeout to your .bashrc or wherever you have it implemented. And then re-source that init file (e.g. with source .bashrc) and try running the script again. Commented Mar 28, 2014 at 14:15

1 Answer 1

5

It's seems your environment $PATH variable does not include /usr/bin/ path or may be timeout binary exists in somewhere else.

So just check path of timeout command using :

command -v timeout

and use absolute path in your script

Ex.

#!/bin/bash
/usr/bin/timeout 2 sleep 5

Update 1#

As per your update in question, it is function created in your shell. you can use absolute path in your script as mentioned in above example.

Update 2# timeout command added from coreutils version => 8.12.197-032bb, If GNU timeout is not available you can use expect (Mac OS X, BSD, ... do not usually have GNU tools and utilities by default).

################################################################################
# Executes command with a timeout
# Params:
#   $1 timeout in seconds
#   $2 command
# Returns 1 if timed out 0 otherwise
timeout() {

    time=$1

    # start the command in a subshell to avoid problem with pipes
    # (spawn accepts one command)
    command="/bin/sh -c \"$2\""

    expect -c "set echo \"-noecho\"; set timeout $time; spawn -noecho $command; expect timeout { exit 1 } eof { exit 0 }"    

    if [ $? = 1 ] ; then
        echo "Timeout after ${time} seconds"
    fi

}

Example:

timeout 10 "ls ${HOME}"

Source

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

4 Comments

which bash version you are using ? and also check /usr/bin/timeout exists or not
Using the absolute path does not help. It then says - /usr/bin/timeout: No such file or directory found
in your script, at the beginning
Now, it says this - Starting AutoMATCH program alling Main-dispatch-ex program filename:

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.