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/;
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/;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With