- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- grep help
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2007 09:39 AM
12-12-2007 09:39 AM
grep help
set -x
run=$(ps -ef | grep ${0} | grep -v grep | wc -l)
echo $run
The above command on server 1 gives the following output:
+ + ps -ef
+ grep ./test
+ wc -l
+ grep -v grep
run=1
+ echo 1
1
The same command on server 2 gives the following output:
+ + ps -ef
+ grep ./test
+ grep -v grep
+ wc -l
run=2
+ echo 2
2
I need some help to find out why the first sever says 1 and the second server says 2. They are both HP-UX.
Thanks,
GBR
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2007 09:52 AM
12-12-2007 09:52 AM
Re: grep help
All indications are that there is a second process running on server2.
However the basic methodology is flawed as ps -ef | grep provides inaccurate results and should never be used to write process kill lists.
UNIX95=1
More accurate process list methodology.
ps -a -o pid,sz,vsz,args -C $FILTER
# this format shows memory use but you get the idea
SEP
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2007 09:54 AM
12-12-2007 09:54 AM
Re: grep help
Are both servers on same OS release and patches?
Rgds...Geoff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2007 09:55 AM
12-12-2007 09:55 AM
Re: grep help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2007 10:02 AM
12-12-2007 10:02 AM
Re: grep help
# cat test
#!/bin/sh
set -x
ps -ef | grep ${0} |grep -v grep > /tmp/test.out
run=$(cat /tmp/test.out|wc -l)
echo $run
Rgds....Geoff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2007 10:14 AM
12-12-2007 10:14 AM
Re: grep help
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:
# cat ./muddy
#!/usr/bin/sh
test -z $0
exit
...Your script will return "2".
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2007 10:16 AM
12-12-2007 10:16 AM
Re: grep help
# run=$(UNIX95= ps -C test -o comm= | wc -l)
# echo $run
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2007 10:26 AM
12-12-2007 10:26 AM
Re: grep help
Oh, H*ll. Disregard my post. I concocted a bogus case! However, naming a script 'test' is poor at best.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2007 04:29 PM
12-12-2007 04:29 PM
Re: grep help
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):
ps -fp 123
Or find all processes running as a particular user:
ps -fu billh
For you example, you add the XPG4 option -C by using the UNIX95 variable:
UNIX95=1 ps -fC test
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:
./test
test
/usr/local/bin/test
../bin/test
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:
#!/usr/bin/sh
MYNAME=${0##*/}
echo "$MYNAME run=$(UNIX95=1 ps -C $MYNAME | wc -l)"
Bill Hassell, sysadmin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2007 06:59 PM
12-12-2007 06:59 PM
Re: grep help
>I need some help to find out why the first sever says 1 and the second server says 2.
Well, remove the "wc -l" so you can see what you are getting.
>Geoff: on the first server, it is running the wc -l prior the grep -v
I think that is a red herring. Pay no attention to the man behind the green curtain. :-)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-24-2007 07:14 AM
12-24-2007 07:14 AM
Re: grep help
I found another thread that did this:
MYPROC=${0##*/}
MYPID=$(UNIX95= ps -C $MYPROC -o pid=)
COUNT=$(echo "$MYPID" | wc -w)
It works great.
GBR