Bash – Timeout a script with stdin

bashtimeout

I need something like this:

timeout -k 4 cat file | myscript.sh

but i get this error:

timeout: invalid time interval ‘cat’
Try 'timeout --help' for more information.

I want to pass stdin to myscript.sh, and kill it after 4s of execution.

Best Answer

Upon reading the output of timeout --help (as suggested by the output in your question), I found that you are not using the proper arguments for the timeout command.

The format of the command is: timeout [OPTION] DURATION COMMAND [ARG]...

And the -k option also takes a DURATION argument. So when using -k,there must be two DURATION arguments.

So a proper format could be: timeout -k 6 4 cat file | myscript.sh

It is not going to change much though. cat doesn't block the TERM signal, so sending a KILL signal isn't necessary. And even if you do kill cat, that is no guarantee that the script terminates. It just means the script will get EOF, in case it tries to read any more from stdin.

You are probably better off putting a timeout on the script instead of the cat command.