Sponsored Content
Full Discussion: Recursive pid script
Top Forums Shell Programming and Scripting Recursive pid script Post 302116202 by jbarnhar on Wednesday 2nd of May 2007 09:59:26 AM
Old 05-02-2007
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 11:48 AM..
 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. Shell Programming and Scripting

Script problem due to recursive directories Help please

Hello everyone , I am looking for a better solution then the one I have created for the my Task The task is: Create an automated script that will check for Uploads in a specified Directory and move them to another specified Directory if the files are completely uploaded. Files are FTP'd to... (2 Replies)
Discussion started by: robertmcol
2 Replies

4. 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

5. Shell Programming and Scripting

pid.cleanup script.

Hi guys! I have a directory in the production environment from which i have to delete files older then 40 minutes with .pid extention. I wrote a script below for the purpose. #!/bin/bash # # Script to delete specific file older than N minutes. # OLDERTHAN="40" #40 minutes ... (6 Replies)
Discussion started by: sajid.shah
6 Replies

6. Shell Programming and Scripting

Recursive Shell script

I'm trying to run the script below recursively but it is generating multiple and simultaneous processes. how do I do to start same script again (last line of my script) only after he had just run entirely? The file name is bachup.sh I am using this command: # / bin / bash / ... (8 Replies)
Discussion started by: sprool
8 Replies

7. Shell Programming and Scripting

Logic needed to recursive looping in the script

Hello i have a requirement where in a file i will get string. The length could be from 1 to 20. if the string is less than 6 characters ( ex: ABCD) . i need to append 'X' on right hand side to make it 6 characters (ex: ABCDXX). if suppose i get the same string from the file as ABCDXX then i... (5 Replies)
Discussion started by: dsdev_123
5 Replies

8. Shell Programming and Scripting

help with recursive handbrake script

I have a batch of mkv video files I would like to transcode to a more compressed format. After spending a significant amount of time experimenting with various parameters I think I have found the handbrake settings that give the best compromise. I now want to construct a simple bash script... (4 Replies)
Discussion started by: jonesal2
4 Replies

9. Shell Programming and Scripting

Help with creating recursive unzip script

Hello. I am trying to create a script that recursively looks at my folder structure, and extracts any .7z files that have not already been extracted yet. I know that all .7z files only have a single .txt file in them, and the only .txt file in any directory would be from that .7z file. I am... (3 Replies)
Discussion started by: Davinator
3 Replies
CHECKPROC(8)						       The SuSE boot concept						      CHECKPROC(8)

NAME
Checkproc - Checks for a process by full path name Pidofproc - Checks for a process by exec base name SYNOPSIS
checkproc [-vLkNz] [-p pid_file] [-i ingnore_file] [-c root] /full/path/to/executable checkproc -n [-vk] name_of_kernel_thread checkproc [-vk] basename_of_executable pidofproc [-LkNz] [-p pid_file] [-i ingnore_file] [-c root] /full/path/to/executable pidofproc -n [-k] name_of_kernel_thread pidofproc [-k] basename_of_executable SYNOPSIS LSB 3.1 pidofproc [-p pid_file] /full/path/to/executable DESCRIPTION
checkproc checks for running processes that use the specified executable. checkproc does not use the pid to verify a process but the full path of the corresponding program which is used to identify the executable (see proc(5)). Only if the inode number (/proc/<pid>/exe) and the full name are unavailable (/proc/<pid>/cmdline) or if the executable changes its zeroth argument, checkproc uses the base name (/proc/<pid>/stat) to identify the running program. Note that if the option -n for kernel thread is given only (/proc/<pid>/stat) is used. For this case a existing symbolic link (/proc/<pid>/exe) indicates that the <pid> is not a kernel thread. Extended functionality is provided by the -p pid_file option (former option -f changed due to the LSB specification). If this option is specified, checkproc tries to check the pid read from this file instead of the default (/var/run/<basename>.pid). The pid read from this file is compared against the pids of the processes that uses the specified binary. If the option -k is specified, checkproc works like killproc that is that if the if the pid_file does not exist, checkproc assumes that the daemon is not running. It is possible to use a process identity number instead of a pid file. For the possibility of having two different sessions of one binary program, the option -i ignore_file allows to specify a pid file which pid number is used to ignore all processes of corresponding process session. Note that the behaviour above is changed by the option -k. With this option, the pid read from the pid file is the only used pid (see killproc(8)) and with this option also exit codes like startproc(8) or killproc(8) are used. Without this option, the pid read from the pid file is used to search the process table for a process with an executable that matches the specified pathname. In order to avoid confu- sion with stale pid files, a not up-to-date pid will be ignored (see startproc(8)). The option -v makes checkproc print out verbose messages. The same happens if pidofproc LSB variant is used. This version also accepts also the base name only of a executable. Note that this disables the comparision of the inodes of the executable and the information found within the proc table (see proc(5)). REQUIRED
/full/path/to/executable or name_of_kernel_thread Specifies the executable which processes should be found, or alternatively, if the option Or alternated, if option -n is used, the name of the kernel thread. This argument is always required. OPTIONS
-k This option makes checkproc work like killproc(8) which changes the operation mode, e.g. the exit status of the program will be that of killproc(8). Without this option, checkproc works like startproc (8) and finds all processes with an executable that matches the specified pathname, even if a given pid file (see option -p) isn't up-to-date. Nevertheless it uses its own exit status (see section EXIT CODES). -L This option causes symlinks to be followed, as the like-named option in ls(1). Note: for the file name the original name of the program is used instead of the name of the symbolic link. -p pid_file Former option -f changed due to the LSB specification.) Use an alternate pid file instead of the default /var/run/<basename>.pid. If the option is specified and the pid_file does not exist, checkproc assumes that the daemon is not running. It is possible to use a process identity number instead of a pid file. -i ignore_file The pid found in this file is used as session id of the same binary program which should be ignored by checkproc. -c root Change root directory to root for services which have been started with this option by startproc(8). -n This option indicates that a kernel thread should be checked. In this case not the executable with its full path name is required but the name of the kernel thread. -N With this option the location of the executable is checked about NFS file system and if true the stat(2) system call is not applied on the exe symbolic link under /proc(5). Otherwise checkproc or pidofproc could be locked if the corresponding NFS server is cur- rently not online or available. This implies that the inode number check between the exectuable on the command line and the exectu- able of the exec symbolic link will be skipped. -q This option is ignored. -v Verbose output. -z This option causes checkproc to see processes even if they are in the zombie state. Without this option zombies are handled as not existent because such a process isn't alive but listed in the process table and waits on its parent process. EXAMPLE
checkproc /usr/sbin/sendmail returns all pids of running sendmail processes. checkproc -p /var/myrun/lpd.pid /usr/sbin/lpd returns the command line or the basename of the process pid found in /var/run/lpd.pid. EXIT CODES
The exit codes without the option -k have the following LSB conform conditions: 0 Program is running 1 No process but pid file found 3 No process and no pid file found 101 Wrong syntax on command line 102 Other errors If the option -k is used checkproc uses exit codes like startproc(8) or killproc(8) do: 0 Program is running 1 Generic or unspecified error 2 Invalid or excess argument(s) 4 Insufficient privilege(s) 5 Program is not installed 7 Program is not running in some cases a message is send to standard error or, if no standard error available, syslogd(8) is used. NOTE
checkproc together with the option -v just like pidof(8). The only difference is the usage of an available pid file. Only if no usable pid is given with a pid file the process table will be scanned. BUGS
Identifying a process based on the executable file and the corresponding inode number only works if the process stays alive during start- proc's execution. Processes rewriting their zeroth argument or shell scripts (the inode number of the shell executable file is not identi- cal to that of the script file) may not be identified by a filename path. FILES
/proc/ path to the proc file system (see proc(5)). /etc/init.d/ path to the SuSE boot concept script base directory as required by the Linux Standard Base Specification (LSB) (see init.d(7)). SEE ALSO
startproc(8), killproc(8), insserv(8), init.d(7), kill(1), skill(1), killall(8), killall5(8), signal(7), proc(5). COPYRIGHT
1994-2005 Werner Fink, 1996-2005 SuSE GmbH Nuernberg, Germany. AUTHOR
Werner Fink <werner@suse.de> 3rd Berkeley Distribution Nov 10, 2000 CHECKPROC(8)
All times are GMT -4. The time now is 07:05 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy