General
Showing results for 
Search instead for 
Do you mean 

pid in shell script again...

Frequent Advisor

pid in shell script again...

hi if thr r two process id s from the following command how to pick the second process id.
l=`ps ux | awk '/mojo/ && !/awk/ {print $2}'`
echo $l;
And if the o/p is
17665
17867
how to store this in an array and retrieve each one of thm whn needed
3 REPLIES
Esteemed Contributor [Founder]

Re: pid in shell script again...

Hi

l=$(ps -C mojo -o pid= | sed -n "2p" )

rgds
HGH
Honored Contributor [Founder]

Re: pid in shell script again...

Hi

You can write a perl script as below.

#!/usr/local/bin/perl
@test = `ps ux | awk '/mojo/ && !/awk/ {print $2}'`;
foreach $test (@test)
{
print "$test\n";
}

save it to a file by name pid.pl
and chmod +x pid.pl
and invoke it
./pid.pl

HTH
Padma
Highlighted
Esteemed Contributor [Founder]

Re: pid in shell script again...

#!/bin/bash -vx

# Pick 2nd (last) PID.
l=`ps ux | awk '/root/ && !/awk/ {print $2}' | tail -1`
echo $l

# Place PIDs in array ary
l=`ps ux | awk '/root/ && !/awk/ {print $2}'`
ary=($l)

Vic
There are 10 kinds of people, one that understands binary and one that doesn't.