Preparing a list of spawned processes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Preparing a list of spawned processes
# 1  
Old 10-21-2010
Preparing a list of spawned processes

Hi All

I'm currently trying to develop a script which will find the child processes of a process ID already passed to the script.

I then need the script to look for spawned processes of these child processes and so on until it can't find any more.

For example

At the moment, I have to manually do this on the server -

Code:
ps -ef | grep 7133 | grep -v grep
USER  7134  7133  0 08:12:38 ?        0:19 child process 1
USER 7133  7128  0 08:12:38 ?        0:00 main processes

I need to find child/spawned processes of 7133 so straight away, I need to grep for 7134.

Code:
ps -ef | grep 7134 | grep -v grep
USER  7137  7134 0 08:12:38 ?        0:19 child process 2
USER 7134  7133  0 08:12:38 ?        0:00 child process 1

Next, I get 7137

You get the idea. There are also cases where one parent process spawns multiple child processes and I need to be able to trace these down individually.

I then need to build a list of all these process ids so that I can kill -9 them.

I've build a script part way and thought I'd be able to assign the processes IDs to a variable using something like this:

Code:
        REP1=$(ps -ef | grep report | grep -v grep | awk '{print $2}')
        REP2=$(ps -ef | grep $REP1 | grep -v grep | awk '{print $2}')
        REP3=$(ps -ef | grep $REP2 | grep -v grep | awk '{print $2}')
        REP4=$(ps -ef | grep $REP3 | grep -v grep | awk '{print $2}')
        REP5=$(ps -ef | grep $REP4 | grep -v grep | awk '{print $2}')

This has failed to work because of multiple processes numbers appearing on the same line.

The script is using KSH.

Any help would be greatly appreciated (as I'm quickly running out of hair!)

TIA
# 2  
Old 10-21-2010
What Operating System and version are you running?
# 3  
Old 10-21-2010
Oops -forgot, it's Solaris 9 although I'm hoping the script will be portable to OEL/RHEL5
# 4  
Old 10-21-2010
Do you get the commands "ptree" and/or "pkill" with that release?
# 5  
Old 10-21-2010
Hi Methyl

I can execute both commands (these are new to me).

If I run ptree 7133 (the process I want to kill) I get the following:

Code:
6737  /corp_sys/oas/1.0/glliv/middle/oracle/app/product/10.1.2.0.2/application_server
  6738  /corp_sys/oas/1.0/glliv/middle/oracle/app/product/10.1.2.0.2/application_server
    7128  /bin/sh /corp_sys/oas/1.0/glliv/middle/oracle/app/product/10.1.2.0.2/applicatio
      7133  tee -a reports.log
        7134  /corp_sys/oas/1.0/glliv/middle/oracle/app/product/10.1.2.0.2/application_server

However, it also lists parent processes... how do I get around that?
# 6  
Old 10-21-2010
Try:

Code:
pgrep -P 7133
# If that list is ok, then
pkill -P 7133

This User Gave Thanks to methyl For This Post:
# 7  
Old 10-22-2010
Almost got there! - not quite though - pgrep -P isn't reporting any further child processes...

Code:
>% ptree 7133
6737  /corp_sys/oas/1.0/glliv/middle/oracle/app/product/10.1.2.0.2/application_server
  6738  /corp_sys/oas/1.0/glliv/middle/oracle/app/product/10.1.2.0.2/application_server
    7128  /bin/sh /corp_sys/oas/1.0/glliv/middle/oracle/app/product/10.1.2.0.2/applicatio
      7133  tee -a reports.log
        7134  /corp_sys/oas/1.0/glliv/middle/oracle/app/product/10.1.2.0.2/application_server
          18480 /corp_sys/oas/1.0/glliv/middle/oracle/app/product/10.1.2.0.2/application_server
>% pgrep -P 7133
7134

Should pgrep -P be reporting proc ID 18480 as well?



---------- Post updated 22-10-10 at 08:55 AM ---------- Previous update was 21-10-10 at 02:57 PM ----------




I've managed to get it working (sort of).

Part of the exercise is to kill off the parent process of the problem processes anyway using another script. Therefore, once these have been killed, I'm only left with the proceses from 7133 downwards.

I can now kill them using something like this:

Code:
        rm /tmp/rep_srv_proc.tmp
        rm /tmp/rep_srv_proc.tmp_tmp
        REP1=$(ps -ef | grep report | grep -v grep | awk '{print $2}')
        ptree $REP1 | awk '{print $1}' > /tmp/rep_srv_proc.tmp_tmp
        ptree $REP1 | awk '{print "kill -9 "$1}' > /tmp/rep_srv_proc.tmp
        chmod 777 /tmp/rep_srv_proc.tmp
        clear
        echo "-----------------------------"
        echo "Do you wish to kill the following processes?:" 
        cat /tmp/rep_srv_proc.tmp_tmp
                echo "-----------------------------"
        read answer
            case $answer in
                yes|Yes|Y|y)
                    /tmp/rep_srv_proc.tmp
                    echo "Processes have been killed..."
                    ;;
                no|n|N|NO)
                    clear 
                    echo "-----------------------------"
                    echo "You've chosen not to kill any processes..."
                    exit
                    ;;
                esac

Thanks for the pointer Methyl.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

List processes using my memory

Here is the output of top command So you see 99% of memory is in use -> Mem: 66005468k total, 65662548k used, How can I find out all processes consuming this 99% memory in descending order of consumption i.e. starting with processes eating more memory. I need the total of the output to... (3 Replies)
Discussion started by: mohtashims
3 Replies

2. UNIX for Advanced & Expert Users

List all background processes

How do I list the process in a Unix based system which are running in background? The following are options that I'm aware of, but they may not be appropiate. a. using ps -ef , and getting records of processes for which STATUS='S'(uninterruptible sleep) b. using jobs -l, and filtering... (5 Replies)
Discussion started by: kumarjt
5 Replies

3. UNIX for Advanced & Expert Users

Kill a list of processes

I am trying to kill a list of processes. I have found these two ways to list a group of process id's on a single line. How would I go about killing all of these processes all on one line? $ ps aux | grep 6243 | grep "a.out" | awk '{printf "%s ",$2}'ps aux | grep 6243 | grep "a.out" | awk... (8 Replies)
Discussion started by: cokedude
8 Replies

4. Solaris

Get list of running network processes

Hello All I am trying to get a list of process or applications runninging on the network only. I should emphasize that im not interested in the application or process if its not using the network. I tried the good old netstat comand, but im not able to figure out how to list the running... (8 Replies)
Discussion started by: busi386
8 Replies

5. UNIX for Dummies Questions & Answers

List processes that are running on other hosts

Hi: How to list processes from all hosts, as opposed to the one you are working at? "ps ux" appears to list processes of the user on a single host only. Thanks. N.B Phil (5 Replies)
Discussion started by: phil518
5 Replies

6. Shell Programming and Scripting

Need to list 3 days old processes

ps -xfu <user name> this command line will list all the process currently running for <user name>. I need to filter this output. I need all the process which are running for more than 3 days(excluding demon/sys process) . The list should include PID, PPID, STIME, process/command. I am using... (20 Replies)
Discussion started by: Sriranga
20 Replies

7. SuSE

List of processes/ which ones to stop

Hi there, I've install a testserver with SLES 11.0! I'll install/test XEN + WebServer not all things at the moment! In a first time, I'd like to stop all unuse processes... but I don't understand all processes! As someone a list of all processes with his signification and which should/could... (3 Replies)
Discussion started by: hiddenshadow
3 Replies

8. Programming

how to get list of processes

Hi, How can I get a list of all the running processes, in C? (13 Replies)
Discussion started by: yaron
13 Replies

9. Linux

How to receive results from processes spawned on external machines using SSH

I am trying to get the number of cpus on a farm of linux boxes (about 100 of them) by 'sshing' to each of them and checking their /proc/cpuinfo file. So I have a local script localscript.sh on each of those 100 machines which retrieves the number of cpus in it by using its /proc/cpuinfo file.... (1 Reply)
Discussion started by: waavman
1 Replies

10. Shell Programming and Scripting

Is there any cmd to kill a process including its childs ( or sub processes spawned by

Dear Unix Gurus, Here is my query. If i start a script,it inturn calls many other scripts ..and most of them continue to run in parallel. Suppose,if i want to stop my script for some reason,i need to kill -9 each of the processes running.It becomes clumsy if the sub processes r more. ... (15 Replies)
Discussion started by: gvsreddy_539
15 Replies
Login or Register to Ask a Question