linux bash timeout

http://www.digitalinternals.com/unix/unix-linux-run-command-with-timeout/500/

There are two ways to send a SIGKILL signal to the process from the timeout utility. The first way is by specifying the default signal to be sent using the following syntax.

$ timeout -s KILL 1m /path/to/slow-command arg1 arg2

The second way is to first send the SIGTERM after the initial timeout. Then, wait for another timeout and send a SIGKILL to the process if it’s still running. This can be done using the following syntax.

$ timeout -k 30 1m /path/to/slow-command arg1 arg2

The process is sent a SIGTERM signal after 60 seconds. If it is still running, then a SIGKILL signal is sent after another 30 seconds.

In the event that the timeout utility is not available, the below 1-liner can be used as an alternative.

$ /path/to/slow-command arg1 arg2 & sleep 60; kill $!

The slow-command is started as a background process. The sleep command will then pause till the timeout duration. In our case, it will sleep for 60 seconds. Once 60 seconds has elapsed, the kill command will send a SIGTERM signal to the slow-command process. The ‘$!‘ shell variable will give the PID of the last background job.

上一篇:java设计模式:观察者模式


下一篇:[kuangbin带你飞]专题一 简单搜索(回顾)