Linux CLI tips & tricks, shell scripting

run commands with timeout

Sometimes you might have use cases, when specific command or script should be run no longer than given amount of time. In those cases you should run commands with timeout.

This command will allow you to terminate command if it reaches given time limit, e.g.:

[root@linux ~]# timeout 3 sleep 4; echo $?
[root@linux ~]# timeout 3 sleep 2; echo $?
[root@linux ~]#

Just add “timeout X” in front of any further command and you are good to go!