Operating System - HP-UX
1835020 Members
2632 Online
110073 Solutions
New Discussion

UNIX95= and ps command dropping lines

 
Chris Sarjent
New Member

UNIX95= and ps command dropping lines

I'm writing a Perl script to gather some OS information. I need to execute a ps command with the -o option to format the output. Therefore, I have to use the UNIX95= syntax. When I issue the command from the UNIX prompt it works fine, but from with a Perl script I also loose a line for some processes. There appears to be a pattern, but I don't know why.

Here is an example of what I'm talking about.
Command line output

(fin_hp11 fin11c03) $ ps -ef | grep tgcgs
tgcgs 15109 1 0 Sep 9 ? 1:11 fics -LI 7 -v
tgcgs 15294 15109 0 Sep 9 ? 0:00 fics -LI 7 -v
tgcgs 15494 1 0 Sep 9 ? 0:47 oraclefin11t01 (DESCRIPTION=(LOCAL=YES)(ADDRESS=(PROTOCOL=beq)))
fin23127 25031 20864 1 11:43:39 pts/1 0:00 grep tgcgs
(fin_hp11 fin11c03) $ UNIX95= ps -A -o ruser,time,comm,args | grep tgcgs
tgcgs 01:11 fics fics -LI 7 -v
tgcgs 00:00 fics fics -LI 7 -v
tgcgs 00:47 oraclefin11t01 oraclefin11t01 (DESCRIPTION=(LOCAL=YES)(ADDRESS=(PROTOCOL=beq)))
fin23127 00:00 grep grep tgcgs

Here is the output from the Perl script.
(fin_hp11 fin11c03) $ cpu_usage
input line is tgcgs 01:11 fics fics -LI 7 -v
input line is tgcgs 00:00 fics fics -LI 7 -v
tgcgs: 2 71
User is tgcgs and CPU time used is 0 days 0 hours 1 minutes 11 seconds

As you can see the grep line is gone and that it expected. However, the process showing the connection to the Oracle database is gone too and that isn't expected.

Here is the command from within the Perl script. Once this is resolved the grep and grep -v will not exist in this command. They only exist here for testing purposes.
{ open (PROCESSES, "UNIX95= ps -e -o user,time,comm,args | grep tgcgs | grep -v grep |") or die "Cannot execute the ps command\n";
}

Thanks in advance for any assistance you can provide.
Chris Sarjent
6 REPLIES 6
Jonathan Fife
Honored Contributor

Re: UNIX95= and ps command dropping lines

If you're using the UNIX95 directive you might as well use -U or -u to specify the userid. Do you get different results if you do that?

UNIX95= ps -u tgcgs -o user,time,comm,args
Decay is inherent in all compounded things. Strive on with diligence
Chris Sarjent
New Member

Re: UNIX95= and ps command dropping lines

Jonathan - Thnnks for the quick reply. If I specify the -U (upper case only) option it works for that user. However, I want all users so my final solution won't be restricting the list by a user. However, if the uidlist can be a wild card that might work as well.
Jonathan Fife
Honored Contributor

Re: UNIX95= and ps command dropping lines

You can list users separated by a comma using the -u option. If you truly want all users, though, you won't be doing any of the greps. If it worked using -u without the greps I would be hopeful that it would work using -A or -e as well.

One other thought -- logically run through your script just to make sure it isn't accidentally ignoring the last line of input. I've run into situations like that once or twice.
Decay is inherent in all compounded things. Strive on with diligence
Chris Sarjent
New Member

Re: UNIX95= and ps command dropping lines

I thought I had done all the checking, but I was wrong. After a closer look at my script and the command I was using to test from the command line I see that the command line specified "ruser", but the Perl script specified "user". Using the -U with the user ID and some print lines pointed out the difference rather clearly that I couldn't see before.

Thanks!
Chris Sarjent
New Member

Re: UNIX95= and ps command dropping lines

See prior update for solution.
Bill Hassell
Honored Contributor

Re: UNIX95= and ps command dropping lines

The line:
"UNIX95= ps -e -o user,time,comm,args | grep tgcgs | grep -v grep |"

will always be erratic in operation. grep has no concept of a field -- it is just matching strings. So a user might be named bill but your script will match billh and billng and waybill and billy, thus retrieving incorrect matches. And bill could be contained in the command line arguments.

ps knows user names exactly (no wrong matches) and can have a long list in the -U option such as:

-U bill,joe,ashley,darren,ellen,bob,bobo

Also, it isn't clear in the man page but you can assign the -o options a new column heading INCLUDING nothing. Also, -o comm is a subset of -o args but if you need to isolate the actual command, you can use both, but args must be last. So if your line is something like:

"UNIX95= ps -U bill,joe,ashley,darren,ellen,bob,bobo -o user= -o time= -o comm= -o args="

Now you won't have to filter out the header line and you'll get all the selected users. The problem with -e is that you also get all the junk from root and other system and application processes.


Bill Hassell, sysadmin