Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Script to kill all child process for a given PID Post 17971 by sanjay92 on Thursday 21st of March 2002 02:28:46 PM
Old 03-21-2002
Thanks for your suggestion.
I have written a generic script based on your suggestion.
Code:
#########################################

#!/bin/ksh
# This script will kill all the child process id for a  given pid
#Store the current Process ID, we don't want to kill the current executing process id
CURPID=$$

# This is process id, parameter passed by user
ppid=$1

if [ -z $ppid ] ; then
   echo No PID given.
   exit;
fi

arraycounter=1
while true
do
        FORLOOP=FALSE
        # Get all the child process id
        for i in `ps -ef| awk '$3 == '$ppid' { print $2 }'`
        do
                if [ $i -ne $CURPID ] ; then
                        procid[$arraycounter]=$i
                        arraycounter=`expr $arraycounter + 1`
                        ppid=$i
                        FORLOOP=TRUE
                fi
        done
        if [ "$FORLOOP" = "FALSE" ] ; then
           arraycounter=`expr $arraycounter - 1`
           ## We want to kill child process id first and then parent id's
           while [ $arraycounter -ne 0 ]
           do
             kill -9 "${procid[$arraycounter]}" >/dev/null
             arraycounter=`expr $arraycounter - 1`
           done
         exit
        fi
done

#####################################

added code tags for readability --oombera

Last edited by oombera; 02-18-2004 at 04:42 PM..
sanjay92
 

10 More Discussions You Might Find Interesting

1. Programming

Child Process PID

Hi, Can anybody solve this query? A parent process forks 2 child processes. How does the child process know it's PID without the parent process sending it. Apart from the "ps-ef" option, what other options are there if any? (2 Replies)
Discussion started by: skannan
2 Replies

2. Shell Programming and Scripting

In ksh, how does an in-line child sub-process get its own PID?

This is not the same as a few of the other posted items dealing with sub-process pids (that I saw anyway). If zot contains: echo "main mypid: $$ - lastpid: $!" ( echo "block mypid: $$ - lastpid: $! - ppid: $PPID" ps -ef > xxx sleep 5 echo "block mypid: $$ - lastpid: $! - ppid:... (6 Replies)
Discussion started by: MichLab
6 Replies

3. Shell Programming and Scripting

grab PID of a process and kill it in a script

#!/bin/sh who echo "\r" echo Enter the terminal ID of the user in use: echo "\r" read TERM_ID echo "\r" ps -t $TERM_ID | grep sh echo "\r" echo Enter the process number to end: echo "\r" read PID echo "\r" kill -9 $PID What this code does is ultimately grab the PID of a users sh... (6 Replies)
Discussion started by: psytropic
6 Replies

4. UNIX for Dummies Questions & Answers

How to Kill process with dynamic PID?

Hello, I have problem with killing red5 process running on linux server. As this process is continuously changing its PID so it can't be killed with "kill -9 PID" command. First I used following command to list RED5 process ps aux | grep red5 which showed me root 5832 0.0 0.0 4820 756pts/0... (1 Reply)
Discussion started by: ninadgac
1 Replies

5. Shell Programming and Scripting

How to Kill process with dynamic PID?

Hello, I have problem with killing red5 process running on linux server. As this process is continuously changing its PID so it can't be killed with "kill -9 PID" command. First I used following command to list RED5 process ps aux | grep red5 which showed me root 5832 0.0 0.0 4820 756pts/0... (4 Replies)
Discussion started by: ninadgac
4 Replies

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

7. Shell Programming and Scripting

Kill all child process of a script

Hi guys i have a problem with a script... this script creates differents GUI with YAD... well i want that when i press the "Cancel" button on this graphical interface all the child process and even the same script should be killed #!/bin/bash function gui_start { local choice="" ... (4 Replies)
Discussion started by: maaaaarco
4 Replies

8. Shell Programming and Scripting

Getting child process id for a given PID

HI Am trying to get child process id for a PID using ksh.. ps -ef | grep xntpd root 3342472 2228308 0 12:17:40 - 0:00 /usr/sbin/xntpd root 4522024 6488316 0 12:18:56 pts/0 0:00 grep xntpd root 6291614 3342472 0 12:17:40 - 0:00 /usr/sbin/xntpd Here now i... (1 Reply)
Discussion started by: Priya Amaresh
1 Replies

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

10. Shell Programming and Scripting

Shell script to report file size, pid and also kill the process

Hi All, Looking for a quick LINUX shell script which can continuously monitors the flle size, report the process which is creating a file greater than certain limit and also kill that process. Can someone please help me on this? (4 Replies)
Discussion started by: vasavimacherla
4 Replies
PROC_GET_STATUS(3)							 1							PROC_GET_STATUS(3)

proc_get_status - Get information about a process opened byproc_open(3)

SYNOPSIS
array proc_get_status (resource $process) DESCRIPTION
proc_get_status(3) fetches data about a process opened using proc_open(3). PARAMETERS
o $process - The proc_open(3) resource that will be evaluated. RETURN VALUES
An array of collected information on success, and FALSE on failure. The returned array contains the following elements: +---------+--------------------------------------+---+ |element | | | | | | | | | type | | | | | | | | description | | | | | | +---------+--------------------------------------+---+ |command | | | | | | | | | | | | | string | | | | | | | | The command string that was passed | | | | to proc_open(3). | | | | | | | pid | | | | | | | | | | | | | int | | | | | | | | process id | | | | | | |running | | | | | | | | | | | | | bool | | | | | | | | | | | | TRUE if the process is still run- | | | | ning, FALSE if it has terminated. | | | | | | |signaled | | | | | | | | | | | | | bool | | | | | | | | | | | | TRUE if the child process has been | | | | terminated by an uncaught signal. | | | | Always set to FALSE on Windows. | | | | | | |stopped | | | | | | | | | | | | | bool | | | | | | | | | | | | TRUE if the child process has been | | | | stopped by a signal. Always set to | | | | FALSE on Windows. | | | | | | |exitcode | | | | | | | | | | | | | int | | | | | | | | The exit code returned by the | | | | process (which is only meaningful if | | | | running is FALSE). Only first call | | | | of this function return real value, | | | | next calls return -1. | | | | | | |termsig | | | | | | | | | | | | | int | | | | | | | | The number of the signal that | | | | caused the child process to termi- | | | | nate its execution (only meaningful | | | | if signaled is TRUE). | | | | | | |stopsig | | | | | | | | | | | | | int | | | | | | | | The number of the signal that | | | | caused the child process to stop its | | | | execution (only meaningful if | | | | stopped is TRUE). | | | | | | +---------+--------------------------------------+---+ SEE ALSO
proc_open(3). PHP Documentation Group PROC_GET_STATUS(3)
All times are GMT -4. The time now is 02:04 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy