<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Capturing hung command in a script. in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/capturing-hung-command-in-a-script/m-p/5666939#M53693</link>
    <description>Just 1 word... Brilliant!!! Thanks a lot Matti.. it works like a wonder for me.</description>
    <pubDate>Wed, 23 May 2012 15:36:57 GMT</pubDate>
    <dc:creator>sbahra</dc:creator>
    <dc:date>2012-05-23T15:36:57Z</dc:date>
    <item>
      <title>Capturing hung command in a script.</title>
      <link>https://community.hpe.com/t5/operating-system-linux/capturing-hung-command-in-a-script/m-p/5662103#M53679</link>
      <description>&lt;P&gt;We are working on a script which needs to monitor if APPHA is hung or not. For this, we manually have to run command "hastatus -sum" If there is an output out of this command then we assume that the APPHA is still running and if the command hangs then it means that APPHA is hung.&lt;/P&gt;&lt;P&gt;What string can be added in a the script to check if "hastatus -sum" command has hung or not? If hung then it should send out an email.&lt;/P&gt;</description>
      <pubDate>Fri, 18 May 2012 14:52:55 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/capturing-hung-command-in-a-script/m-p/5662103#M53679</guid>
      <dc:creator>sbahra</dc:creator>
      <dc:date>2012-05-18T14:52:55Z</dc:date>
    </item>
    <item>
      <title>Re: Capturing hung command in a script.</title>
      <link>https://community.hpe.com/t5/operating-system-linux/capturing-hung-command-in-a-script/m-p/5662411#M53681</link>
      <description>&lt;P&gt;If you run "hastatus -sum" normally in your script and it hangs, the script won't proceed either. You'll need to implement a &lt;EM&gt;timeout&amp;nbsp; &lt;/EM&gt;that stops the hanging command and causes the mail to be sent if the command does not complete in a reasonable amount of time.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you Google with keywords "shell script timeout", I'm sure you will find many examples.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If "hastatus -sum" provides a non-zero return status (also known as return code or exit code) if it's interrupted and zero if it completes successfully, something like this might do the job:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;#!/bin/sh

WAITTIME=20 # seconds to wait for hastatus -sum to complete

# run the hastatus command in the background, discarding any output
hastatus -sum &amp;gt;/dev/null 2&amp;gt;&amp;amp;1 &amp;amp;
HASTATUS_PID=$!

# set up a timeout that will kill the hastatus command when 
# $WAITTIME seconds has passed, unless it has completed before that.
(sleep $WAITTIME; kill $HASTATUS_PID 2&amp;gt;/dev/null) &amp;amp;
TIMEOUT_PID=$!

# wait for the hastatus command to either complete or get killed; read its return status
wait $HASTATUS_PID
RESULT=$?

# if the timeout is still running, stop it (ignore any errors)
kill $TIMEOUT_PID 2&amp;gt;/dev/null

# read the return status of the timeout process (we don't need it 
# but running the wait function prevents it from remaining as a 
# zombie process)
wait $TIMEOUT_PID

if [ $RESULT -ne 0 ]
then
    echo "something is wrong with APPHA, result was $RESULT"
    # insert the command to send mail here
else
    echo "APPHA seems to be OK"
fi&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 May 2012 23:59:40 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/capturing-hung-command-in-a-script/m-p/5662411#M53681</guid>
      <dc:creator>Matti_Kurkela</dc:creator>
      <dc:date>2012-05-18T23:59:40Z</dc:date>
    </item>
    <item>
      <title>Re: Capturing hung command in a script.</title>
      <link>https://community.hpe.com/t5/operating-system-linux/capturing-hung-command-in-a-script/m-p/5666939#M53693</link>
      <description>Just 1 word... Brilliant!!! Thanks a lot Matti.. it works like a wonder for me.</description>
      <pubDate>Wed, 23 May 2012 15:36:57 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/capturing-hung-command-in-a-script/m-p/5666939#M53693</guid>
      <dc:creator>sbahra</dc:creator>
      <dc:date>2012-05-23T15:36:57Z</dc:date>
    </item>
  </channel>
</rss>

