7

I want to kill a command after waiting for result for 2 seconds. If the command didn't come up with a result (took too long), the command should stop. I have gone through the documents and tested the following command:

timeout --kill-after=2 ls /mnt/ftp/;
echo $?;

Or

timeout -k 2 ls /mnt/ftp/;
echo $?;

However, I am getting this error:

timeout: invalid time interval ‘ls’

Note: The command below stops after timeout period by doesn't kill the process:

timeout 2 ls /mnt/ftp/;

1 Answer 1

8

The documentation for timeout is tricky in a way. If you refer it carefully it says

$ timeout --help
Usage: timeout [OPTION] DURATION COMMAND [ARG]...
  or:  timeout [OPTION]
Start COMMAND, and kill it if still running after DURATION.

Here the option -k itself takes a value followed by a value needed for DURATION also, so your command should two values back to back when using -k as below. The error is thrown because DURATION is a mandatory argument to be used.

timeout --kill-after=2 2 ls /mnt/ftp/;

The first option --kill-after=2 is part of the OPTION flag to the command which takes value 2 and the DURATION itself takes a value 2 separately.

timeout -k 2 2 ls /mnt/ftp/;
Sign up to request clarification or add additional context in comments.

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.