Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error using timeout command - invalid time interval

Tags:

bash

timeout

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/;
like image 517
Hamid Reza Avatar asked Feb 12 '18 05:02

Hamid Reza


1 Answers

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/;
like image 73
Inian Avatar answered Oct 20 '22 16:10

Inian