How to Run a Command with Time Limit in Bash Linux

Posted on April 6, 2019 at 2:48 pm

You can use “timeout” command to run a program and auto-kill it after N seconds:

timeout --signal=KILL 3 whois

The above command will auto-kill whois if it runs for more than 3 seconds.

Here is an example output in a Debian server:

root@server:~# timeout --signal=KILL 3 whois

More information here:

Probably, it is better this solution:

timeout -k 5 10 someCommand

Timeout sends TERM signal after the 10 seconds. If someCommand didn’t respond to TERM (e.g. it could block the TERM signal) then timeout sends KILL signal after 5 more seconds (i.e. at the 15th second since the start of execution).


Simpler option is also just:

timeout 5 someCommand

Updated on November 29, 2021 at 1:00 pm

Receive updates via email

Other Posts

Updated Posts