Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to get the first and last words from a long ps -ef?

Status
Not open for further replies.

srjordan

Technical User
Jun 14, 2004
8
CA
Hi everyone,

I'm trying to get the first and last strings of a process from the result of a ps -ef command in AIX. For example, if I do a ps -ef|grep java, here's one of the results: (I'll spare you the rest of them :) )

was_coo 29212 1 0 Jun 11 pts/2 0:00 /bin/sh /usr/WebSphere/AppServer5/bin/launchClient.sh /usr/bsiapps/coonuans/ear/CooNuans.ear -CCBootstrapPort=2816 -CCclasspath=/usr/bsiapps/coonuans/resources:/usr/bsiapps/coo/resources:/usr/mqm/java/lib/com.ibm.mq.jar:/usr/mqm/java/lib/com.ibm.mqjms.jar:/usr/mqm/java/lib/connector.jar:/usr/mqm/java/lib/jms.jar:usr/mqm/java/lib/jndi.jar:/usr/mqm/java/lib/jta.jar:/usr/mqm/java:/usr/WebSphere/AppServer5/lib/websphere.jar:/usr/WebSphere/AppServer5/lib/ujc.jar:/usr/mqm/java/lib/com.ibm.mq.jar:/usr/mqm/java/lib/com.ibm.mqjms.jar:/usr/mqm/java/lib/connector.jar:/usr/mqm/java/lib/jms.jar:usr/mqm/java/lib/jndi.jar:/usr/mqm/java/lib/jta.jar:/usr/mqm/java:/usr/WebSphere/AppServer5/lib/websphere.jar:/usr/WebSphere/AppServer5/lib/ujc.jar:/usr/mqm/java/lib/com.ibm.mq.jar:/usr/mqm/java/lib/com.ibm.mqjms.jar:/usr/mqm/java/lib/connector.jar:/usr/mqm/java/lib/jms.jar:usr/mqm/java/lib/jndi.jar:/usr/mqm/java/lib/jta.jar:/usr/mqm/java:/usr/WebSphere/AppServer5/lib/websphere.jar:/usr/WebSphere/AppServer5/lib/ujc.jar:/usr/mqm/java/lib/com.ibm.mq.jar:/usr/mqm/java/lib/com.ibm.mqjms.jar:/usr/mqm/java/lib/connector.jar:/usr/mqm/java/lib/jms.jar:usr/mqm/java/lib/jndi.jar:/usr/mqm/java/lib/jta.jar:/usr/mqm/java:/usr/WebSphere/AppServer5/lib/websphere.jar:/usr/WebSphere/AppServer5/lib/ujc.jar:/home/dbclient/sqllib/java/db2java.zip:/home/dbclient/sqllib/java/db2jcc.jar:/home/dbclient/sqllib/function:/home/dbclient/sqllib/java/db2jcc_license_cu.jar:. -CCjar=CooNuansMonitorAppClient.jar

So in this example, I would like to change my ps -ef|grep java to something like ps -ef|grep java|awk '{print $1" "$2}' to get

was_coo 29212

but I also want the last word in the line so that the returned result would be something like

was_coo 29212 -CCjar=CooNuansMonitorAppClient.jar

Also keep in mind that some of the ps -ef results are so long that the end is truncated. (In AIX anyway). I've thought of using "cut" but I can't find how to specify the last position and work back from there.

Anyone have any ideas? Any help would be greatly appreciated.
 
Hey thats cool. Wish I had done it that way when i was writing my Operator scripts for WebSphere. BTW, I hate WebSphere, lol.

___________________________________
--... ...--, Eric.
 
Thanks fjchia for the reply, that gets me a little closer.

I ran the command and got back the expected result for the specific example above, ie, I got back

was_coo 22112 -CCjar=CooNuansMonitorAppClient.jar

which is excellent.

However, a few of the processes have really, really long classpaths that get truncated. For those ones, it returns the really really large classpath for $NF but I need the string at the very end.

If I can get the ps command to display the entire process string than this would be solved.

Do you or anyone else know of a way of getting the whole process without it being truncated in AIX?
 
if on Solaris, try '/usr/ucb/ps -aww'

get more detailed explanation of the options from man.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
I think the command is "ps auww" in AIX as far as I can tell but that didn't work.

When I try ps auww I see the processes but they are truncated. So even without the grep, the output is truncated.

I'm guessing there is a limit to the number of characters that ps can display in aix but I haven't found anything that supports that theory.
 
a Perl solution - sorry!

Code:
#!/usr/bin/perl

$_ = " was_coo 29212     1   0   Jun 11  pts/2  0:00 /bin/sh /usr/WebSphere/AppServer5/bin/launchClient.sh /usr/bsiapps/coonuans/ear/CooNuans.ear -CCBootstrapPort=2816 -CCclasspath=/usr/bsiapps/coonuans/resources:/usr/bsiapps/coo/resources:/usr/mqm/java/lib/com.ibm.mq.jar:/usr/mqm/java/lib/com.ibm.mqjms.jar:/usr/mqm/java/lib/connector.jar:/usr/mqm/java/lib/jms.jar:usr/mqm/java/lib/jndi.jar:/usr/mqm/java/lib/jta.jar:/usr/mqm/java:/usr/WebSphere/AppServer5/lib/websphere.jar:/usr/WebSphere/AppServer5/lib/ujc.jar:/usr/mqm/java/lib/com.ibm.mq.jar:/usr/mqm/java/lib/com.ibm.mqjms.jar:/usr/mqm/java/lib/connector.jar:/usr/mqm/java/lib/jms.jar:usr/mqm/java/lib/jndi.jar:/usr/mqm/java/lib/jta.jar:/usr/mqm/java:/usr/WebSphere/AppServer5/lib/websphere.jar:/usr/WebSphere/AppServer5/lib/ujc.jar:/usr/mqm/java/lib/com.ibm.mq.jar:/usr/mqm/java/lib/com.ibm.mqjms.jar:/usr/mqm/java/lib/connector.jar:/usr/mqm/java/lib/jms.jar:usr/mqm/java/lib/jndi.jar:/usr/mqm/java/lib/jta.jar:/usr/mqm/java:/usr/WebSphere/AppServer5/lib/websphere.jar:/usr/WebSphere/AppServer5/lib/ujc.jar:/usr/mqm/java/lib/com.ibm.mq.jar:/usr/mqm/java/lib/com.ibm.mqjms.jar:/usr/mqm/java/lib/connector.jar:/usr/mqm/java/lib/jms.jar:usr/mqm/java/lib/jndi.jar:/usr/mqm/java/lib/jta.jar:/usr/mqm/java:/usr/WebSphere/AppServer5/lib/websphere.jar:/usr/WebSphere/AppServer5/lib/ujc.jar:/home/dbclient/sqllib/java/db2java.zip:/home/dbclient/sqllib/java/db2jcc.jar:/home/dbclient/sqllib/function:/home/dbclient/sqllib/java/db2jcc_license_cu.jar:. -CCjar=CooNuansMonitorAppClient.jar";

m|^ ?([^ ]* \d+).*( [^ ]*)$|;

print "$1$2";


Kind Regards
Duncan
 
That's great!

I also came up with a perl solution since "ps" has a 2048 character limit.

This script will give you all the java processes running on a box but will leave out all classpath stuff giving you (hopefully) the information you'll need to actually identify the process. It will also cross reference the uid of the process with the /etc/passed file to display the username running the process.

You will need the Proc::processTable perl module installed for this to run.

#!/usr/bin/perl -w


#Use for formatting the output in a nice format
format STDOUT =
@<<<<<<<< @##### @##### @<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$user[0],$pid_print,$ppid_print,$pid_start,$cmd_to_print
.

use Proc::processTable; # The perl module that allows
# us to access the process table directly

my $t = new Proc::processTable;

open(fileIN,"/etc/passwd") or dienice ("Can't open /etc/passwd: $!\n");
@passwdFile = <fileIN>;
close (fileIN);

my $process_count = 0;

foreach my $process (@{$t->table})
{
if (grep {/java/} $process->cmndline)
{
$process_count = $process_count+1;
foreach my $userinfo (@passwdFile)
{
chomp($userinfo);
@user=split(/:/,$userinfo,6);
if ($process->uid == $user[2])
{
@cmd=split(//,$process->cmndline);
my $size=@cmd;
my $num_chars_to_print = 30;
$cmd_to_print = "";
while ($num_chars_to_print > 0)
{
$cmd_to_print = $cmd_to_print . $cmd[$size - $num_chars_to_print];
$num_chars_to_print = $num_chars_to_print - 1;
}
$pid_print = $process->pid;
$ppid_print = $process->ppid;
$pid_start = scalar(localtime($process->start));
write STDOUT;
}
}
}
}
print "\n","Number of java processes currently running is: ",$process_count,"\n\n";
exit
 
srjordan,

You can build a hash for the uid lookup with this:

while(($name, $passwd, $uid) = getpwent )
{
$namelookup{$uid} = $name;
}

before your loop. Then a simple $namelookup{$process->uid} will give you the name.



Rod Knowlton
IBM Certified Advanced Technical Expert pSeries and AIX 5L

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top