Skip to content

MountainField/bash-timeout

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bash Timeout

bash-timeout is a command and also a bash function in order to terminate the target command if the target command does not finish within the duration specified beforehand. The input via either redirection ( < FILE ) or pipe ( | ) are transferred to the target command transparently. The exit status of the target command is retained if the target command finishes within the duration.

The timeout in GNU coreutils is a similar command which has the timeout capability. In shell script, bash-timeout make the script simpler than GNU timeout. See comparison.md for more details.

Installing

  • via pip

      $ pip install bash-timeout
    

Usage

  • Use as a linux/unix command

    $ bash-timeout 10s sleep 20s
  • Use as a bash function

    source bash-timeout
    timeout 10s sleep 20s
  • An example in bash script

    source bash-timeout
    
    function very_expensive_task() {
        ...
    }
    
    if timeout 10s  very_expensive_task  ; then
        echo "successfully done"
    else
        echo "the task failed or timed out"
    fi

Characteristics

  1. Status code

    1. Return error code if time out

      $ bash-timeout 10s sleep 20s
      $ echo $? #=> 1 or more 
    2. Retain the target command status code if the target command finishes within the duration

      1. Normal exit

        $ bash-timeout 10s ls /bin
        $ echo $? #=> 0
      2. Exit with error

        $ ls /FOOBAR
        $ echo $? #=> 2
        $ bash-timeout 10s ls /FOOBAR
        $ echo $? #=> 2
  2. Input and Output

    • Retain output

      $ echo abc #=> abc 
      $ bash-timeout 10s echo abc #=> abc 
    • Retain input via pipe

      $ echo abc | cat #=> abc 
      $ echo abc | bash-timeout 10s cat #=> abc 
    • Retain input via redirection

      $ echo abc > abc.txt
      $ cat < abc.txt  #=> abc 
      $ < abc.txt cat  #=> abc 
      $ bash-timeout 10s cat < abc.txt  #=> abc 
      $ bash-timeout < abc.txt 10s cat  #=> abc 
      $ < abc.txt bash-timeout 10s cat  #=> abc 

Author

License

This project is licensed under the MIT License - see the LICENSE.txt file for details

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Acknowledgments

We express our sincere thanks to Scott Trent for reviewing documents.