{"id":78,"date":"2011-01-10T13:25:36","date_gmt":"2011-01-10T11:25:36","guid":{"rendered":"https:\/\/www.melen.org\/u\/jan\/wp\/?p=78"},"modified":"2011-02-12T08:17:49","modified_gmt":"2011-02-12T06:17:49","slug":"timeout-for-a-command-called-from-a-shell-script","status":"publish","type":"post","link":"https:\/\/www.melen.org\/u\/jan\/wp\/?p=78","title":{"rendered":"Timeout for a command called from a shell script"},"content":{"rendered":"<p>If a called program stop&#8217;s responding it causes the whole shell script to stop and wait for indefinite amount of time for the called program to finish. One way to avoid this is described below where simple timeout is created with help of sleep command and if the command is not able to finish during that time a kill is called to stop the execution of the command.<\/p>\n<pre>\r\n    #\/bin\/sh\r\n    # Author: Jan Melen-jan(at)melen.org\r\n    #\r\n    TIMEOUT=600\r\n    SIGNAL=\"INT\"\r\n\r\n    &lt;fill your command and params here&gt; &amp;\r\n    PID=$!\r\n    if [ \"${PID}\" != \"0\" ]; then\r\n        (sleep \"${TIMEOUT}\";  kill \"${SIGNAL}\" \"${PID}\") &amp;\r\n        KILLER=$!\r\n        wait \"${PID}\" 2&gt;\/dev\/null\r\n        R=$?\r\n        kill -HUP \"${KILLER}\" 2&gt;\/dev\/null\r\n        if [ $R -ne 0 ]; then\r\n            echo \"*** ERROR: Command exited with an error ***\"\r\n        fi\r\n    fi\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If a called program stop&#8217;s responding it causes the whole shell script to stop and wait for indefinite amount of time for the called program to finish. One way to avoid this is described below where simple timeout is created with help of sleep command and if the command is not able to finish during [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/www.melen.org\/u\/jan\/wp\/index.php?rest_route=\/wp\/v2\/posts\/78"}],"collection":[{"href":"https:\/\/www.melen.org\/u\/jan\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.melen.org\/u\/jan\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.melen.org\/u\/jan\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.melen.org\/u\/jan\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=78"}],"version-history":[{"count":4,"href":"https:\/\/www.melen.org\/u\/jan\/wp\/index.php?rest_route=\/wp\/v2\/posts\/78\/revisions"}],"predecessor-version":[{"id":87,"href":"https:\/\/www.melen.org\/u\/jan\/wp\/index.php?rest_route=\/wp\/v2\/posts\/78\/revisions\/87"}],"wp:attachment":[{"href":"https:\/\/www.melen.org\/u\/jan\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=78"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.melen.org\/u\/jan\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=78"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.melen.org\/u\/jan\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=78"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}