Kill Parent/ Child processes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Kill Parent/ Child processes
# 1  
Old 02-15-2012
Kill Parent/ Child processes

I am trying to kill PIDs that are tied to a KSH "load_sqlplus" and I am using the below code

Code:
 
LIST_PID=`ps -ef | grep -i "load_sqlplus" | grep -v grep | awk '{print $2}'`
if [ "${LIST_PID}" != '' ]; then
    echo "Processes killed" "PID : " $LIST_PID 
    kill -9 $LIST_PID
else
    echo "Nothing to Kill" 
fi

The issue i am facing is there are child processes that are tied to PIDs that falls in the above category and
those are not getting killed.
In other words when i execute the above script I also want to kill any child PIDs associated with the PIDs
getting killed..
Any input will be greatly appreciated
# 2  
Old 02-15-2012
I would be very careful about killing processes that are banging up against a database. Some will argue that you should attempt to kill any process with a TERM or HUP signal before resorting to 'kill -9' as it gives the process a chance to finish gracefully.

That said, if you decide that you still want to kill all of the processes, and any descendants of those processes, that match a pattern, you might have to resort to something like the script below. There might be some 'pstree-like' command out there that does it, but if I knew of one it escapes me at the moment.

Code:
#!/usr/bin/env ksh
ps -elf | awk -v me=$$ -v pat=${1:-foo} '
    function printchain( list,      i, a, n )
    {
        n = split( list, a, " " );
        for( i = 1; i <= n; i++ )
        {
            klist = sprintf( "%s%s ", klist, a[i] );
            printchain( childrenof[a[i]] );
        }
    }

    /-v me/ { next; }
    match( $0, pat ) { parents[++pidx] = $4; }  # parent pids to list

    { childrenof[$5] = childrenof[$5] " " $4 }

    END {
        for( i = 1; i <= pidx; i++ )
        {
            klist = parents[i] " ";
            printchain( childrenof[parents[i]] );
            if( !match( " " klist " ", " " me " " ) )
                printf( "kill these: %s\n", klist );
        }
    }
'
exit

The script accepts a pattern to search the output from ps for (be careful, you could easily over match) and lists all of the PIDs that match, and the descendents; one group per line. It tries to be smart about not listing itself or related processes; your milage might vary on whether it is successful at that or not. It should be easy to extend this to actually issue the kill commands, but I'll stay away from posting any such code.
# 3  
Old 02-16-2012
Thanks for the Inuput..Greatly Appreciated
# 4  
Old 02-17-2012
I think the pgrep and pkill commands should be able to help you on your quest. Be careful killing database processes.
# 5  
Old 02-17-2012
Please post what Operating System and version you have and what Shell you are using. Not all have "pgrep", "ptree" or "pkill".

Never ever issue "kill -9" to a process which might be connected to a database. The only exception I know is when you need to forcibly shut down a database where sessions are stuck on I/O (e.g. after a disc failure).

In your case "kill -15" has a good chance of killing the child processes too.
The "kill -9" could easily leave the child process parentless and looping.

Depending on the circumstances it is usually better to use database tools to disconnect a client rather than crashing the process.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

System should not kill the child process when parent id is 1

HI i would like to know how i can simulate a shell scripts for my requirement. example Server name child Process id Parent Process id Vpesh 16013 15637 Server name child Process id Parent Process id Vpesh 16014 15637 Server name child... (1 Reply)
Discussion started by: vpesh
1 Replies

2. Shell Programming and Scripting

forking a child process and kill its parent to show that child process has init() as its parent

Hi everyone i am very new to linux , working on bash shell. I am trying to solve the given problem 1. Create a process and then create children using fork 2. Check the Status of the application for successful running. 3. Kill all the process(threads) except parent and first child... (2 Replies)
Discussion started by: vizz_k
2 Replies

3. Programming

fork(), parent and child processes???

Hi friends, I have a small question regarding unix system call fork, I hope you will solve my problem. Here is the small program $ cat fork1.c #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { int pid; int x = 0; x = x + 1; pid = fork(); if(pid < 0) {... (2 Replies)
Discussion started by: gabam
2 Replies

4. Shell Programming and Scripting

Creating a pipe using parent and child processes

Hello, I am trying to create a pipe that will direct stdout to in side of the pipe, and stdin to the out side of the pipe - I created two child processes to handle this. However, my pipe doesn't seem to be working correctly. Did I use execv() correctly? Command1 and command2 represent the two... (3 Replies)
Discussion started by: jre247
3 Replies

5. UNIX for Dummies Questions & Answers

Kill child processes, when parent is "bash"

Consider this simple command line bash -c 'echo $$ ; sleep 10000'This will print the newly created bash PID and sleep for a long time. If I go to another terminal and do something like ps -flax | grep leepI'll see something like 501 92418 91910 0 0:00.00 ttys000 0:00.00 bash -c echo $$... (5 Replies)
Discussion started by: teras
5 Replies

6. UNIX for Dummies Questions & Answers

Need help to kill parent and all of its sub processes

Hi, I am writing korn shell script. My requirement is, i have to kill the parent process and all of its child processes. Can some one please help me on this? Thanks in advance for your help.. (1 Reply)
Discussion started by: Sheethal
1 Replies

7. UNIX for Advanced & Expert Users

How to find all the child processes of a parent process

Hi I am trying to see if there are some options in ps command or if there is a shell script which basically shows you all the processes spawned by a parent process , then all the processes of its child processes and so on down the hierarchy may be like a tree structure. It might be a generic... (6 Replies)
Discussion started by: clifford
6 Replies

8. Shell Programming and Scripting

Parent/Child Processes

Hello. I have a global function name func1() that I am sourcing in from script A. I call the function from script B. Is there a way to find out which script called func1() dynamically so that the func1() can report it in the event there are errors? Thanks (2 Replies)
Discussion started by: yoi2hot4ya
2 Replies

9. UNIX for Dummies Questions & Answers

kill parent and child

Hello all, I have gone through the search and looked at posting about idle users and killing processes. Here is my question I would like to kill an idle user ( which I can do) but how can I asure that all of his process is also killed whit out tracing his inital start PID. I have tried this on a... (4 Replies)
Discussion started by: larry
4 Replies

10. UNIX for Dummies Questions & Answers

what are parent and child processes all about?

I don't follow what these are... this is what my text says... "When a process is started, a duplicate of that process is created. This new process is called the child and the process that created it is called the parent. The child process then replaces the copy for the code the parent... (1 Reply)
Discussion started by: xyyz
1 Replies
Login or Register to Ask a Question