The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Recursive FTP -- here at last. Perderabo Shell Programming and Scripting 52 03-25-2009 12:15 PM
Script problem due to recursive directories Help please robertmcol Shell Programming and Scripting 2 04-27-2008 07:00 PM
recursive nice value nhatch UNIX for Advanced & Expert Users 1 07-18-2007 12:29 PM
recursive rcp Nicol Shell Programming and Scripting 6 11-06-2003 11:52 AM
Recursive FTP aslamg UNIX for Dummies Questions & Answers 1 03-08-2001 04:27 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-02-2007
jbarnhar jbarnhar is offline
Registered User
  
 

Join Date: May 2007
Posts: 3
Recursive pid script

I'm trying to create a script that allows me to determine all the pid's that spawned from an original process(ie - who's running this command)....

I've created a script that searches the processes running based on an argument passed the script. It then is to get the parent pid and look that up....It should recursively look up each subsequent parent pid until it can't find any....

I need to be able to pass a variable, parent pid, into a ps -f -p command, but it keeps failing.....

Help!! Any ideas??

Here is the script......

#!/bin/ksh
touch running.out
pid_file=pid_file.lst
ppid_file=ppid_file.lst
while [ -f running.out ]
do
echo "checking for $1 session"
ps -ef|grep $1|grep -v root|cut -f5 -d" " > $pid_file

if [ -s $pid_file ];
then
echo "$1 session found"
while read item;
do
echo "Looking for parent pid - $item"
ps -f -p$item|cut -f5 -d" " > $ppid_file
echo "$ppid_file"
while [ -s $ppid_file ]
do
while read item;
do
ps -f -p$item|cut -f5 -d" " > $ppid_file
echo "$ppid_file"
done < $ppid_file
done
done < $pid_file
else
echo "$1 session is not on the system"
rm running.out
fi

done
rm $pid_file
rm $$ppid_file

---- the error I'm getting is: ps --p needs an argument

Last edited by jbarnhar; 05-02-2007 at 10:48 AM..
  #2 (permalink)  
Old 05-02-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Your shell script is very involved with several loopings.
It would be time consuming to make it work.
Whithout going thru your logic, I can advise you in two things:
1) The problem you are having with the "ps --p needs an argument" is
because you are using a variable that is empty/null.
Try displaying (echo) all parameters before executing each command.
Example:
echo "item = <${item}>"
ps -f -p$item
There is a large probability that the variable "item" is empty/null at one point.
2) Your script still has an error -- missing a double quote ("):
echo $1 session found"
  #3 (permalink)  
Old 05-02-2007
jbarnhar jbarnhar is offline
Registered User
  
 

Join Date: May 2007
Posts: 3
Thanks for the response Shell Life.....

The line above the ps -f -p$item|cut -f5 -d" " includes the $item variable which does have a value....Here is the output when the script is run.....

# ./tunnel_ppid.sh ora_mmon_olap

checking for ora_mmon_olap session
ora_mmon_olap session found
Looking for parent pid - 6008
ppid_file.lst
ps: option requires an argument -- p
usage: ps [-edaxzflP] [-u ulist] [-g glist] [-p plist] [-t tlist] [-R prmgroup] [-Z psetidlist]
ppid_file.lst
  #4 (permalink)  
Old 05-02-2007
jbarnhar jbarnhar is offline
Registered User
  
 

Join Date: May 2007
Posts: 3
Thanks for the help everyone.....

I ended up figuring out the solution pending your assistance....Here is the updated code if anyone cares.....LOL

#!/bin/ksh
touch running.out
pid_file=pid_file.lst
while [ -f running.out ]
do
ps -ef|grep $1|grep -v root|cut -f5 -d" " > $pid_file
ps -ef|grep $1|grep -v root
if [ -s $pid_file ];
then
while read item;
do
xcmd=$(ps -f -p$item|grep $item|cut -c16-20)
echo $(ps -f -p$item|grep $item)
if [ -n $xcmd ];
then
touch looping.out
while [ -f looping.out ]
do
xcmd2=$(ps -f -p$xcmd|grep $xcmd|cut -c16-20)
echo $(ps -f -p$xcmd|grep $xcmd)
if [ -n $xcmd2 -a "${xcmd2}" != ' 0' ];
then
xcmd=$(ps -f -p$xcmd2|grep $xcmd2|cut -c16-20)
echo $(ps -f -p$xcmd2|grep $xcmd2)
else
echo "No additional Parent PID found"
rm looping.out
exit
fi
done
else
echo "No Parent PID found"
fi
done < $pid_file
else
echo "$1 session is not on the system"
rm running.out
fi
done


The output generated looks like this:

# ./tunnel_ppid.sh sqlplus
oracle 7171 3591 0 15:06:29 pts/ta 00:00 /U03/app/oracle/product/10.2.0/bin/sqlplus -s
oracle 7171 3591 0 15:06:29 pts/ta 0:00 /U03/app/oracle/product/10.2.0/bin/sqlplus -s
oracle 3591 5120 0 14:42:57 pts/ta 0:00 /bin/ksh ./refresh_tables.sh
oracle 5120 5069 0 16:24:00 pts/ta 0:00 -su
dc01928 5069 5068 0 16:23:20 pts/ta 0:00 -sh
root 5068 1261 0 16:23:19 pts/ta 0:00 telnetd
root 1261 1 0 16:12:59 ? 0:00 /usr/sbin/inetd -l
root 1 0 0 16:11:49 ? 0:06 init
root 0 0 0 16:11:49 ? 0:22 swapper
No additional Parent PID found

Which shows you the progression of who is ultimately running a process.....
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:25 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0