<?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: grep help in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/grep-help/m-p/5083980#M441105</link>
    <description>Using ps and grep together is the worst, most unstable way there is to locate a process by name. grep cannot tell the difference between programs test, tester, 123test, and will also fail by finding a user ID called abctest or a group called qatest. And the reason you have to use grep -v is that grep will also look at the command line where some other program is using some parameters that test buried in the string.&lt;BR /&gt; &lt;BR /&gt;So disregard all Unix beginner manuals that show the ps|grep combinations (and waste time looking at every process) and instead, use the 100% accurate features of ps. Did you know that you find an exact PID such as 123 (and not accidently find PID 1234 or 4123): &lt;BR /&gt; &lt;BR /&gt;ps -fp 123&lt;BR /&gt; &lt;BR /&gt;Or find all processes running as a particular user:&lt;BR /&gt; &lt;BR /&gt;ps -fu billh&lt;BR /&gt; &lt;BR /&gt;For you example, you add the XPG4 option -C by using the UNIX95 variable:&lt;BR /&gt; &lt;BR /&gt;UNIX95=1 ps -fC test&lt;BR /&gt; &lt;BR /&gt;This *always* works. Be sure to type this command on one line and do NOT export UNIX95. The above construct temporarily defines UNIX95 just for ps. To rewrite your script correctly, you must not use ${0} as your script's name. The reason is that you can start your script in many different ways:&lt;BR /&gt; &lt;BR /&gt;./test&lt;BR /&gt;test&lt;BR /&gt;/usr/local/bin/test&lt;BR /&gt;../bin/test&lt;BR /&gt; &lt;BR /&gt;and of course, $0 will be the complete string with all the (extra) directory junk. Instead, isolate your script's name like this and drop the -ef options:&lt;BR /&gt; &lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;MYNAME=${0##*/}&lt;BR /&gt;echo "$MYNAME run=$(UNIX95=1 ps -C $MYNAME | wc -l)"&lt;BR /&gt;</description>
    <pubDate>Thu, 13 Dec 2007 00:29:15 GMT</pubDate>
    <dc:creator>Bill Hassell</dc:creator>
    <dc:date>2007-12-13T00:29:15Z</dc:date>
    <item>
      <title>grep help</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/grep-help/m-p/5083972#M441097</link>
      <description>#!/bin/sh&lt;BR /&gt;set -x&lt;BR /&gt;run=$(ps -ef | grep ${0} | grep -v grep | wc -l)&lt;BR /&gt;echo $run&lt;BR /&gt;&lt;BR /&gt;The above command on server 1 gives the following output:&lt;BR /&gt;&lt;BR /&gt;+ + ps -ef&lt;BR /&gt;+ grep ./test&lt;BR /&gt;+ wc -l&lt;BR /&gt;+ grep -v grep&lt;BR /&gt;run=1&lt;BR /&gt;+ echo 1&lt;BR /&gt;1&lt;BR /&gt;&lt;BR /&gt;The same command on server 2 gives the following output:&lt;BR /&gt;&lt;BR /&gt;+ + ps -ef&lt;BR /&gt;+ grep ./test&lt;BR /&gt;+ grep -v grep&lt;BR /&gt;+ wc -l&lt;BR /&gt;run=2&lt;BR /&gt;+ echo 2&lt;BR /&gt;2&lt;BR /&gt;&lt;BR /&gt;I need some help to find out why the first sever says 1 and the second server says 2.  They are both HP-UX.&lt;BR /&gt;&lt;BR /&gt;Thanks,&lt;BR /&gt;GBR</description>
      <pubDate>Wed, 12 Dec 2007 17:39:36 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/grep-help/m-p/5083972#M441097</guid>
      <dc:creator>GBR</dc:creator>
      <dc:date>2007-12-12T17:39:36Z</dc:date>
    </item>
    <item>
      <title>Re: grep help</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/grep-help/m-p/5083973#M441098</link>
      <description>Shalom,&lt;BR /&gt;&lt;BR /&gt;All indications are that there is a second process running on server2.&lt;BR /&gt;&lt;BR /&gt;However the basic methodology is flawed as ps -ef | grep provides inaccurate results and should never be used to write process kill lists.&lt;BR /&gt;&lt;BR /&gt;UNIX95=1&lt;BR /&gt;&lt;BR /&gt;More accurate process list methodology.&lt;BR /&gt;&lt;BR /&gt;ps -a -o pid,sz,vsz,args -C $FILTER&lt;BR /&gt;# this format shows memory use but you get the idea&lt;BR /&gt;&lt;BR /&gt;SEP</description>
      <pubDate>Wed, 12 Dec 2007 17:52:59 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/grep-help/m-p/5083973#M441098</guid>
      <dc:creator>Steven E. Protter</dc:creator>
      <dc:date>2007-12-12T17:52:59Z</dc:date>
    </item>
    <item>
      <title>Re: grep help</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/grep-help/m-p/5083974#M441099</link>
      <description>Well - on the first server, it is running the wc -l prior the grep -v&lt;BR /&gt;&lt;BR /&gt;Are both servers on same OS release and patches?&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Rgds...Geoff</description>
      <pubDate>Wed, 12 Dec 2007 17:54:41 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/grep-help/m-p/5083974#M441099</guid>
      <dc:creator>Geoff Wild</dc:creator>
      <dc:date>2007-12-12T17:54:41Z</dc:date>
    </item>
    <item>
      <title>Re: grep help</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/grep-help/m-p/5083975#M441100</link>
      <description>Imho a better maybe unique name for your script might help. The name test could be part of another actively running process on the system or may have been passed as an argument. Picking a distinct name might help resolve the issue.</description>
      <pubDate>Wed, 12 Dec 2007 17:55:41 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/grep-help/m-p/5083975#M441100</guid>
      <dc:creator>Sandman!</dc:creator>
      <dc:date>2007-12-12T17:55:41Z</dc:date>
    </item>
    <item>
      <title>Re: grep help</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/grep-help/m-p/5083976#M441101</link>
      <description>This works:&lt;BR /&gt;&lt;BR /&gt;# cat test&lt;BR /&gt;#!/bin/sh&lt;BR /&gt;set -x&lt;BR /&gt;ps -ef | grep ${0} |grep -v grep &amp;gt; /tmp/test.out&lt;BR /&gt;run=$(cat /tmp/test.out|wc -l)&lt;BR /&gt;echo $run&lt;BR /&gt;&lt;BR /&gt;Rgds....Geoff</description>
      <pubDate>Wed, 12 Dec 2007 18:02:40 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/grep-help/m-p/5083976#M441101</guid>
      <dc:creator>Geoff Wild</dc:creator>
      <dc:date>2007-12-12T18:02:40Z</dc:date>
    </item>
    <item>
      <title>Re: grep help</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/grep-help/m-p/5083977#M441102</link>
      <description>Hi:&lt;BR /&gt;&lt;BR /&gt;I am in agreement with Sandman.  Using the name "test" mimics the inbuilt 'test' command.  Run this script in the background and then run yours:&lt;BR /&gt;&lt;BR /&gt;# cat ./muddy&lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;test -z $0&lt;BR /&gt;exit&lt;BR /&gt;&lt;BR /&gt;...Your script will return "2".&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...&lt;BR /&gt;</description>
      <pubDate>Wed, 12 Dec 2007 18:14:07 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/grep-help/m-p/5083977#M441102</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2007-12-12T18:14:07Z</dc:date>
    </item>
    <item>
      <title>Re: grep help</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/grep-help/m-p/5083978#M441103</link>
      <description>How about leveraging the XPG4 capabilities of ps(1) i.e.&lt;BR /&gt;&lt;BR /&gt;# run=$(UNIX95= ps -C test -o comm= | wc -l)&lt;BR /&gt;# echo $run</description>
      <pubDate>Wed, 12 Dec 2007 18:16:52 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/grep-help/m-p/5083978#M441103</guid>
      <dc:creator>Sandman!</dc:creator>
      <dc:date>2007-12-12T18:16:52Z</dc:date>
    </item>
    <item>
      <title>Re: grep help</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/grep-help/m-p/5083979#M441104</link>
      <description>Hi (again):&lt;BR /&gt;&lt;BR /&gt;Oh, H*ll.  Disregard my post.  I concocted a bogus case!  However, naming a script 'test' is poor at best.&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Wed, 12 Dec 2007 18:26:54 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/grep-help/m-p/5083979#M441104</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2007-12-12T18:26:54Z</dc:date>
    </item>
    <item>
      <title>Re: grep help</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/grep-help/m-p/5083980#M441105</link>
      <description>Using ps and grep together is the worst, most unstable way there is to locate a process by name. grep cannot tell the difference between programs test, tester, 123test, and will also fail by finding a user ID called abctest or a group called qatest. And the reason you have to use grep -v is that grep will also look at the command line where some other program is using some parameters that test buried in the string.&lt;BR /&gt; &lt;BR /&gt;So disregard all Unix beginner manuals that show the ps|grep combinations (and waste time looking at every process) and instead, use the 100% accurate features of ps. Did you know that you find an exact PID such as 123 (and not accidently find PID 1234 or 4123): &lt;BR /&gt; &lt;BR /&gt;ps -fp 123&lt;BR /&gt; &lt;BR /&gt;Or find all processes running as a particular user:&lt;BR /&gt; &lt;BR /&gt;ps -fu billh&lt;BR /&gt; &lt;BR /&gt;For you example, you add the XPG4 option -C by using the UNIX95 variable:&lt;BR /&gt; &lt;BR /&gt;UNIX95=1 ps -fC test&lt;BR /&gt; &lt;BR /&gt;This *always* works. Be sure to type this command on one line and do NOT export UNIX95. The above construct temporarily defines UNIX95 just for ps. To rewrite your script correctly, you must not use ${0} as your script's name. The reason is that you can start your script in many different ways:&lt;BR /&gt; &lt;BR /&gt;./test&lt;BR /&gt;test&lt;BR /&gt;/usr/local/bin/test&lt;BR /&gt;../bin/test&lt;BR /&gt; &lt;BR /&gt;and of course, $0 will be the complete string with all the (extra) directory junk. Instead, isolate your script's name like this and drop the -ef options:&lt;BR /&gt; &lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;MYNAME=${0##*/}&lt;BR /&gt;echo "$MYNAME run=$(UNIX95=1 ps -C $MYNAME | wc -l)"&lt;BR /&gt;</description>
      <pubDate>Thu, 13 Dec 2007 00:29:15 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/grep-help/m-p/5083980#M441105</guid>
      <dc:creator>Bill Hassell</dc:creator>
      <dc:date>2007-12-13T00:29:15Z</dc:date>
    </item>
    <item>
      <title>Re: grep help</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/grep-help/m-p/5083981#M441106</link>
      <description>You of course can use "ps -fp $$" to get the current process.  And $PPID to get your parent.&lt;BR /&gt;&lt;BR /&gt;&amp;gt;I need some help to find out why the first sever says 1 and the second server says 2.&lt;BR /&gt;&lt;BR /&gt;Well, remove the "wc -l" so you can see what you are getting.&lt;BR /&gt;&lt;BR /&gt;&amp;gt;Geoff: on the first server, it is running the wc -l prior the grep -v&lt;BR /&gt;&lt;BR /&gt;I think that is a red herring.  Pay no attention to the man behind the green curtain. :-)</description>
      <pubDate>Thu, 13 Dec 2007 02:59:49 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/grep-help/m-p/5083981#M441106</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2007-12-13T02:59:49Z</dc:date>
    </item>
    <item>
      <title>Re: grep help</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/grep-help/m-p/5083982#M441107</link>
      <description>Thanks everynone.&lt;BR /&gt;&lt;BR /&gt;I found another thread that did this:&lt;BR /&gt;&lt;BR /&gt;MYPROC=${0##*/}&lt;BR /&gt;MYPID=$(UNIX95= ps -C $MYPROC -o pid=)&lt;BR /&gt;COUNT=$(echo "$MYPID" | wc -w)&lt;BR /&gt;&lt;BR /&gt;It works great.&lt;BR /&gt;&lt;BR /&gt;GBR</description>
      <pubDate>Mon, 24 Dec 2007 15:14:54 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/grep-help/m-p/5083982#M441107</guid>
      <dc:creator>GBR</dc:creator>
      <dc:date>2007-12-24T15:14:54Z</dc:date>
    </item>
  </channel>
</rss>

