Using grep to extract PID


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using grep to extract PID
# 1  
Old 03-14-2010
Error Using grep to extract PID

Hi unix experts!

I need help to solve the following problem:

I'm getting the prstat info:
stats=`prstat 0 1` #dont worry about how many processes are running I'm aware that no all processes will be listed without the -n (specify number)

then I'm doing the following to extract the PIDs:
pids=`echo "$stats" | awk '{print $1}'`
pids=`echo $pids | sed -e 's/PID//' | sed -e 's/Total://'` #to remove the Total and PID lines which are not required

Then I'm iterating through the PID's and trying to grep for each of the PID's my running ps. That way I'm able to look up things like the arguments and command rather than just the PID. The aim of doing this is eventually I want to replace the PID output from prstat to something more meaningful with command and args info for that process. This is the code for that:

for i in $pids
do
output=`ps -eaf -o pid,comm,args | grep ^' '*$i' '`
...
The output I'm getting is :
Usage: grep -hblcnsviw pattern file . . .
prstat_script.sh: *7 : not found

If I put in the escape characters i.e. output=`ps -eaf -o pid,comm,args | grep \^\' \'*$i\' \'` then I get:
grep: can't open '*7'
grep: can't open '

This is obviously because grep sees a space and tries to use '$i' as the file name. This is not what I want to achieve though.

If I run the non-escaped version on the command line (not in a bash script) then it works fine and gives me the output i desire, e.g.:

bash-3.00$ ps -eaf -o pid,comm,args | grep ^' '*1008' '

gives the output:

1008 /usr/lib/saf/ttymon /usr/lib/saf/ttymon

Thing is I don't know how to do this in the bash script.

Your help would really be appreciated.

Thanks very much in advance.

Sanjay
# 2  
Old 03-14-2010
Hmm... works for me -

Code:
$ 
$ cat -n f0.sh
     1  #!/bin/bash
     2  pids="2060 2061 2062"
     3  for i in $pids
     4  do
     5    output=`ps eaf -o pid,comm,args | grep ^' '*$i' '`
     6    echo $output
     7  done
$ 
$ ./f0.sh
2060 mingetty /sbin/mingetty tty2
2061 mingetty /sbin/mingetty tty3
2062 mingetty /sbin/mingetty tty6
$

Try awk perhaps ?

Code:
$ 
$ ps eaf -o pid,comm,args | awk '/^ 2060/'
 2060 mingetty        /sbin/mingetty tty2
$ 
$ ps eaf -o pid,comm,args | awk '/^ *2060/'
 2060 mingetty        /sbin/mingetty tty2
$ 
$ cat -n f1.sh
     1  #!/bin/bash
     2  pids="2060 2061 2062"
     3  for i in $pids
     4  do
     5    output=`ps eaf -o pid,comm,args | awk "/^ *$i/"`
     6    echo $output
     7  done
$ 
$ ./f1.sh
2060 mingetty /sbin/mingetty tty2
2061 mingetty /sbin/mingetty tty3
2062 mingetty /sbin/mingetty tty6
$ 
$

tyler_durden
# 3  
Old 03-15-2010
I did get it working in the end as a result of your post durden_tyler. It has to do with the way that you were running the script. I was using sh to run the script rather than executing it using ./

Once I figured that one out I was able to get it to work.

Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

How to grep PID and program name from netstat in AIX?

Hi All, I am using netstat on AIX to grep info on all open connections. However, unlike on Linux(Centos), I do not get the PID and program name using netstat on AIX. I need this info to be clubbed along with the information retrieved using netstat version of AIX. Is there a way this can be... (1 Reply)
Discussion started by: Vipin Batra
1 Replies

2. Shell Programming and Scripting

Error in finding the PID by grep and assigning to a variable

Hello All, I am facing difficulty in getting the PID value & then assigning it to a variable, kindly help me in resolving the issue. Thanks a lot in advance. The custom utility used inside the Test2.sh will process the file in a batch of 10 lines at once and for efficient memory management,... (3 Replies)
Discussion started by: duddukuri
3 Replies

3. Shell Programming and Scripting

script to extract PID

Can anyone explain the below script which is used to extract the PID? Why they are taking from 10 character and getting the sub string? Note:variable pid has been assigned the PID value initially as below. pid=`sed 's@FILNAME@'$destFil'@g' $scriptname |sed 's@SRCFILE@'$sourceFil'@g' |sed... (2 Replies)
Discussion started by: bk_12345
2 Replies

4. Shell Programming and Scripting

grep PID and collect data ?

I have a file current.csv file 2011/05/06 11:12:20 | 16:guest pid=014782 opened Boards 0, 1, 2, 3 2011/05/06 12:23:13 | 16:guest pid=014782 closed 2011/05/06 12:26:05 | 17:guest pid=022383 opened Boards 0, 1, 2, 3 2011/05/06 20:09:04 | 17:guest pid=022383 closed 2011/05/06... (5 Replies)
Discussion started by: sabercats
5 Replies

5. Shell Programming and Scripting

how to grep parent process PID in shell scripting

hi all, for an example: $ ps -ef | grep apache | awk '{ print $2, $3 }' 24073 11784 28021 1 28022 1 28038 1 28041 28040 28045 28041 28047 28041 28040 1 28049 28041 28051 28041 28053 28041 28030 1 28054 28041 28055 28041 28056 28041 28057 28041 (5 Replies)
Discussion started by: raghur77
5 Replies

6. UNIX for Dummies Questions & Answers

determine pid from ps -ef | grep something

Hi all, i have a script called a.sh and within a.sh it calls b.sh. when i do a ps -ef | grep a.sh, i get two sets of number user_test 4225 3250 0 10:31 pts/1 00:00:00 a.sh when i do a ps -ef | grep b.sh, i get two sets of number user_test 4269 4225 22 10:31 pts/1 00:00:45... (17 Replies)
Discussion started by: new2ss
17 Replies

7. UNIX for Dummies Questions & Answers

Need to get pid of a process and have to store the pid in a variable

Hi, I need to get the pid of a process and have to store the pid in a variable and i want to use this value(pid) of the variable for some process. Please can anyone tell me how to get the pid of a process and store it in a variable. please help me on this. Thanks in advance, Amudha (7 Replies)
Discussion started by: samudha
7 Replies

8. Shell Programming and Scripting

How to extract the PID from 'ps -ef'

Hi I am writing a shell script in ksh where I have to grep for a process name , say XYZ from "ps -ef" and then extract it's PID. ps -ef | grep XYZ gives - " int 7738 25734 1 02:00:49 pts/tc 0:00 grep XYZ" I am thinking of replacing one or more occurrences of the space with pipe... (7 Replies)
Discussion started by: nsinha
7 Replies

9. UNIX for Dummies Questions & Answers

Session PID & socket connection pid

1. If I use an software application(which connects to the database in the server) in my local pc, how many PID should be registered? Would there be PID for the session and another PID for socket connection? 2. I noticed (through netstat) that when I logged in using the my software application,... (1 Reply)
Discussion started by: pcx26
1 Replies

10. Programming

printing ppid,child pid,pid

question: for the below program i just printed the value for pid, child pid and parent pid why does it give me 6 values? i assume ppid is 28086 but can't figure out why there are 5 values printed instead of just two! can someone comment on that! #include<stdio.h> #define DIM 8 int... (3 Replies)
Discussion started by: a25khan
3 Replies
Login or Register to Ask a Question