<?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: While Loop Not Exit in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/while-loop-not-exit/m-p/3512941#M844178</link>
    <description>The script is designed to run forever. It keeps on monitoring the process in $COMMAND and sends mail when it stops, and continues monitoring. It even has a flag to send only one notice when the process stops. A script like this would be run in the background. A couple of notes:&lt;BR /&gt; &lt;BR /&gt; PROCS=`ps -ef|grep -v grep|grep ${COMMAND}|wc -l`&lt;BR /&gt; &lt;BR /&gt;has a basic problem. ps and grep are seldom reliable. While $COMMAND is set to "DEVP", the above statement can return several matches because grep never looks at the process name...it looks at the entire line. The grep will match DDEVP, /opt/DEVPDIR/prog and many other strings, whether it is a program name or a parameter. ps is so much more powerful than most admins know. Using ps -ef is a big load on a busy server so let's have ps do the searching in the process table. Change ps to do the searches with 100% accuracy:&lt;BR /&gt; &lt;BR /&gt;COMMAND=DEVP&lt;BR /&gt;UNIX95= ps -C $COMMAND&lt;BR /&gt; &lt;BR /&gt;Now ps always returns EXACTLY the number of processes that have a basename of DEVP. The UNIX95= construct is needed to enable 3 useful options: -C -H -o. ps always supplies a header as line #1, so we have to remember that wc -l is 1 higher than needed.&lt;BR /&gt; &lt;BR /&gt;Throughout the script, the construct $(...) is used for "Command Substitution" except in the PROCS= statement. $(...) is always preferred, so the code would work better like this:&lt;BR /&gt;&lt;BR /&gt;COMMAND=DEVP&lt;BR /&gt;...&lt;BR /&gt;let PROCS=$(UNIX95= ps -C $COMMAND | wc -l)-1&lt;BR /&gt;if test $PROCS -gt 0&lt;BR /&gt;then&lt;BR /&gt;...process is up&lt;BR /&gt;else&lt;BR /&gt;...process is down&lt;BR /&gt;fi&lt;BR /&gt; &lt;BR /&gt;And with all scripts, always test the script by running it in trace mode:&lt;BR /&gt; &lt;BR /&gt;sh -x ./myscript&lt;BR /&gt; &lt;BR /&gt;Now you'll see all the steps and the results.</description>
    <pubDate>Mon, 28 Mar 2005 23:01:14 GMT</pubDate>
    <dc:creator>Bill Hassell</dc:creator>
    <dc:date>2005-03-28T23:01:14Z</dc:date>
    <item>
      <title>While Loop Not Exit</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/while-loop-not-exit/m-p/3512938#M844175</link>
      <description>I tested a wonderful script from sb. of this forum in an attempt to apply it to my situation.   But I can not have the while loop end unless a hard keystroke of Ctrl+C is enforced.   Syntac looks all right.&lt;BR /&gt;&lt;BR /&gt;Here are the lines of script:&lt;BR /&gt;&lt;BR /&gt;***************************************&lt;BR /&gt;#!/bin/sh&lt;BR /&gt;NOTIFIED=0&lt;BR /&gt;COMMAND=DEVP                                  #test DEVP db up status&lt;BR /&gt;MAILBOX=zzzz@wwwyyy.com&lt;BR /&gt;CHECKINTERVAL=10                              #launch every 10 seconds&lt;BR /&gt;while true&lt;BR /&gt;do&lt;BR /&gt;  PROCS=`ps -ef|grep -v grep|grep ${COMMAND}|wc -l`&lt;BR /&gt;  if test ${PROCS} -gt 1                      # cannot use "-eq 1"&lt;BR /&gt;  then&lt;BR /&gt;    if test ${NOTIFIED} -eq 0&lt;BR /&gt;    then&lt;BR /&gt;      echo "Service ${COMMAND} is up again!!\n `date`" | mailx -s "Service Start&lt;BR /&gt;ed" ${MAILBOX}&lt;BR /&gt;      NOTIFIED=1&lt;BR /&gt;    fi&lt;BR /&gt;  else&lt;BR /&gt;    echo "Servie ${COMMAND} is down!!\n `date`" | mailx -s "Service Stopped" ${M&lt;BR /&gt;AILBOX}&lt;BR /&gt;  NOTIFIED=0    # need comment out to avoid unlimited checking/mailxing&lt;BR /&gt;  fi&lt;BR /&gt;  sleep ${CHECKINTERVAL}&lt;BR /&gt;done&lt;BR /&gt;******************************************&lt;BR /&gt;&lt;BR /&gt;All comments are appreciated!&lt;BR /&gt;&lt;BR /&gt;Steven&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 28 Mar 2005 15:45:46 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/while-loop-not-exit/m-p/3512938#M844175</guid>
      <dc:creator>Steven Chen_1</dc:creator>
      <dc:date>2005-03-28T15:45:46Z</dc:date>
    </item>
    <item>
      <title>Re: While Loop Not Exit</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/while-loop-not-exit/m-p/3512939#M844176</link>
      <description>You have a "while true" loop without a break or an exit statement. What do you expect it to do except loop forever? It would help if your listed the conditions upon which you want it to exit.</description>
      <pubDate>Mon, 28 Mar 2005 16:11:21 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/while-loop-not-exit/m-p/3512939#M844176</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2005-03-28T16:11:21Z</dc:date>
    </item>
    <item>
      <title>Re: While Loop Not Exit</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/while-loop-not-exit/m-p/3512940#M844177</link>
      <description>If I'm reading your script correctly, you want to&lt;BR /&gt;check, once every 10 seconds, if a command is &lt;BR /&gt;running and send a mail with the status.&lt;BR /&gt;&lt;BR /&gt;How long you want this to run? In the current form, &lt;BR /&gt;this script would run forever. Now, if that's what&lt;BR /&gt;you want, you could run the script in the background&lt;BR /&gt;and it would keep doing the check you want. If&lt;BR /&gt;you want it to run for few hours, or want it to exit&lt;BR /&gt;under certain conditions, then you will have to insert&lt;BR /&gt;some code that checks this condition at the beginning&lt;BR /&gt;(or before end) of the "while" loop and exit.&lt;BR /&gt;&lt;BR /&gt;- Biswajit&lt;BR /&gt;</description>
      <pubDate>Mon, 28 Mar 2005 18:05:55 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/while-loop-not-exit/m-p/3512940#M844177</guid>
      <dc:creator>Biswajit Tripathy</dc:creator>
      <dc:date>2005-03-28T18:05:55Z</dc:date>
    </item>
    <item>
      <title>Re: While Loop Not Exit</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/while-loop-not-exit/m-p/3512941#M844178</link>
      <description>The script is designed to run forever. It keeps on monitoring the process in $COMMAND and sends mail when it stops, and continues monitoring. It even has a flag to send only one notice when the process stops. A script like this would be run in the background. A couple of notes:&lt;BR /&gt; &lt;BR /&gt; PROCS=`ps -ef|grep -v grep|grep ${COMMAND}|wc -l`&lt;BR /&gt; &lt;BR /&gt;has a basic problem. ps and grep are seldom reliable. While $COMMAND is set to "DEVP", the above statement can return several matches because grep never looks at the process name...it looks at the entire line. The grep will match DDEVP, /opt/DEVPDIR/prog and many other strings, whether it is a program name or a parameter. ps is so much more powerful than most admins know. Using ps -ef is a big load on a busy server so let's have ps do the searching in the process table. Change ps to do the searches with 100% accuracy:&lt;BR /&gt; &lt;BR /&gt;COMMAND=DEVP&lt;BR /&gt;UNIX95= ps -C $COMMAND&lt;BR /&gt; &lt;BR /&gt;Now ps always returns EXACTLY the number of processes that have a basename of DEVP. The UNIX95= construct is needed to enable 3 useful options: -C -H -o. ps always supplies a header as line #1, so we have to remember that wc -l is 1 higher than needed.&lt;BR /&gt; &lt;BR /&gt;Throughout the script, the construct $(...) is used for "Command Substitution" except in the PROCS= statement. $(...) is always preferred, so the code would work better like this:&lt;BR /&gt;&lt;BR /&gt;COMMAND=DEVP&lt;BR /&gt;...&lt;BR /&gt;let PROCS=$(UNIX95= ps -C $COMMAND | wc -l)-1&lt;BR /&gt;if test $PROCS -gt 0&lt;BR /&gt;then&lt;BR /&gt;...process is up&lt;BR /&gt;else&lt;BR /&gt;...process is down&lt;BR /&gt;fi&lt;BR /&gt; &lt;BR /&gt;And with all scripts, always test the script by running it in trace mode:&lt;BR /&gt; &lt;BR /&gt;sh -x ./myscript&lt;BR /&gt; &lt;BR /&gt;Now you'll see all the steps and the results.</description>
      <pubDate>Mon, 28 Mar 2005 23:01:14 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/while-loop-not-exit/m-p/3512941#M844178</guid>
      <dc:creator>Bill Hassell</dc:creator>
      <dc:date>2005-03-28T23:01:14Z</dc:date>
    </item>
  </channel>
</rss>

