nixCraft Linux Forum

nixCraft

Linux / UNIX Tech Support Forum

killing defunct and orphan process

This is a discussion on killing defunct and orphan process within the Shell scripting forums, part of the Development/Scripting category; Hi Team, I need a shell script which will kill the orphan process and defunct process in my server. Please ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 24-06-2009, 11:23 AM
kumarat9pm's Avatar
Senior Member
User
 
Join Date: Jun 2007
Location: Pune,MH,India
OS: RHEL,UBUNTU..
Posts: 447
Thanks: 20
Thanked 20 Times in 18 Posts
Rep Power: 5
kumarat9pm has a spectacular aura about kumarat9pm has a spectacular aura about kumarat9pm has a spectacular aura about
Send a message via Yahoo to kumarat9pm Send a message via Skype™ to kumarat9pm
Default killing defunct and orphan process

Hi Team,
I need a shell script which will kill the orphan process and defunct process in my server. Please help.

Thanks,
Kumar
Reply With Quote
  #2 (permalink)  
Old 24-06-2009, 04:38 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 245 Times in 184 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Why script? There are commands that can do same quickly:
Code:
pkill -u user processname
pkill -u www httpd
pkill -u php php-cgi
Or may be you need something else.. if so please explain in details.
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
  #3 (permalink)  
Old 24-06-2009, 05:26 PM
kumarat9pm's Avatar
Senior Member
User
 
Join Date: Jun 2007
Location: Pune,MH,India
OS: RHEL,UBUNTU..
Posts: 447
Thanks: 20
Thanked 20 Times in 18 Posts
Rep Power: 5
kumarat9pm has a spectacular aura about kumarat9pm has a spectacular aura about kumarat9pm has a spectacular aura about
Send a message via Yahoo to kumarat9pm Send a message via Skype™ to kumarat9pm
Default

Hi,
I am not an admin, I am new to the scripting too, Here is the snippet from server, the shell script I am looking
is to kill all the defunct process listed out in my server.Every day this should be automatically deleted at specfic time.
INDIA#ps-ef | grep defunct
aaaa 29507 29342 0 - ? 0:00 <defunct>
bbbbbbbb 23210 23089 0 - ? 0:00 <defunct>
cccccccc 27298 27287 0 - ? 0:27 <defunct>

script should be like this.
1.use ps_ef | grep defunct
2.get the process id and kill them all
Reply With Quote
  #4 (permalink)  
Old 24-06-2009, 11:35 PM
jaysunn's Avatar
Powered By Linux
User
 
Join Date: Apr 2009
Location: 41.332032,-73.089775
OS: RHEL - OSX
Scripting language: BASH - Learning Ruby
Posts: 602
Thanks: 61
Thanked 80 Times in 72 Posts
Rep Power: 10
jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold
Default Hello Sir

Code:
#!/bin/bash

for x in ps -ef | grep defunct | awk '{print $2}' ; do

kill -9 $x

done
Please test with echo $x in your script before using kill -9 $x

This will echo out the process ID'S of the defunct processes.

Good Luck

Jaysunn
Reply With Quote
  #5 (permalink)  
Old 29-06-2009, 11:43 AM
kumarat9pm's Avatar
Senior Member
User
 
Join Date: Jun 2007
Location: Pune,MH,India
OS: RHEL,UBUNTU..
Posts: 447
Thanks: 20
Thanked 20 Times in 18 Posts
Rep Power: 5
kumarat9pm has a spectacular aura about kumarat9pm has a spectacular aura about kumarat9pm has a spectacular aura about
Send a message via Yahoo to kumarat9pm Send a message via Skype™ to kumarat9pm
Default

Hi,
The script is fine but my worry is the child Process is not killing. i have to delete the parent process to kill the defunc.

Example:
Code:
 ps -ef | grep defunc
Outputs
Code:
     root  2093  2050   0 22:51:29 pts/79      0:00 grep defunc
aaaaaaaa 25552 25537   0        - ?           0:00 <defunct>
  bbbbbbbbbb  2018  2013   0        - ?           0:00 <defunct>
  bbbbbbbbbb  5170  5152   0        - ?           0:18 <defunct>
aaaaaaaa 25548 25537   0        - ?           0:00 <defunct>
aaaaaaaa 25363 25338   0        - ?           0:00 <defunct>
aaaaaaaa 25356 25338   0        - ?           0:01 <defunct>
aaaaaaaa 25373 25338   0        - ?           0:00 <defunct>
aaaaaaaa 25791 25781   0        - ?           0:00 <defunct>
cccccccc  8478  8384   0        - ?           0:00 <defunct>
cccccccc  8466  8384   0        - ?           0:00 <defunct>
aaaaaaaa 25341 25338   0        - ?           0:00 <defunct>
cccccccc  8474  8384   0        - ?           0:00 <defunct>
  bbbbbbbbbb 22242 22232   0        - ?           0:24 <defunct>
aaaaaaaa 25539 25537   0        - ?           0:00 <defunct>
  ddddddddddd 13908 13889   0        - ?           0:00 <defunct>
aaaaaaaa 25543 25537   0        - ?           0:01 <defunct>
ddddddddddd 13952 13911   0        - ?           0:00 <defunct>
  bbbbbbbbbb 17217 17210   0        - ?           0:03 <defunct>
aaaaaaaa 25585 25579   0        - ?           0:01 <defunct>
aaaaaaaa 25813 25781   0        - ?           0:00 <defunct>
  bbbbbbbbbb 17631 17621   0        - ?           0:09 <defunct>
aaaaaaaa 25589 25579   0        - ?           0:00 <defunct>
  bbbbbbbbbb 17724 17717   0        - ?           0:02 <defunct>
  bbbbbbbbbb 18756 18745   0        - ?           0:03 <defunct>
i am not able to kill the child process which is listed above .Hence i changed the script to suport to kill the Parent Process. is this right method to kill the Parent Process?

Note: i tried manually to kill the child Process.It's is not working.., Please let what should I do to kill the child Process instead of Parent Process.
After killing the Process I should get a mail saying these are Process got killed like that..

# vi defunc.sh
Code:
"defunc.sh" 6 lines, 99 characters 
#!/bin/bash
x=0;
for x in `ps -ef | grep defunct | awk '{print $3}'` ; do
echo $x
kill -9 $x
done

"defunc.sh" 6 lines, 98 characters
# ./defunc.sh
Code:
25537
2013
5152
25537
25338
25338
25338
25781
8384
8384
25338
8384
22232
25537
25101
25150
13889
25537
13911
17210
25579
25781
17621
25579
17717
18745


Last edited by nixcraft; 29-06-2009 at 11:51 AM. Reason: [code] tags added
Reply With Quote
  #6 (permalink)  
Old 29-06-2009, 11:50 AM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 245 Times in 184 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Use killall instead of kill:
Code:
killall -9 process-name
OR
Code:
pkill -9 -u user process-name
Edit: I see you want it by pid.. grab all pids and kill them
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
  #7 (permalink)  
Old 30-06-2009, 10:31 AM
raj's Avatar
raj raj is offline
Senior Member
User
 
Join Date: Jun 2005
Location: Hyderabad
OS: Fedora, Debian Linux
Posts: 307
Thanks: 42
Thanked 8 Times in 8 Posts
Rep Power: 6
raj will become famous soon enough raj will become famous soon enough
Default

Read this tread - Killing zombie process

Especially comment where a few scripts and hacks are posted by users.

PHP Code:
#!/usr/bin/bash
#
# Zombie Slayer
#
# zombie_slayer [OPTIONS]
#
# -d
#   Just diplay the zombie processes and exit
#
# -h | help
#   Display help
#
# Default is to show processes and the ask what to kill
#

function Z_Display
{
    echo 
""
    
echo "PID - PPID - State - User - Proc"
    
UNIX95ps -eo pid,ppid,state,user,comm awk 'BEGIN { count=0 } $3 ~ /Z/ { count++; print $1,$2,$3,$4,$5 } END { print "\n" count " Zombie(s) to slay." }'
    
echo ""
}

function 
Z_Kill
{
    
read -"Enter PPID to kill or 'exit' : " SLAY_PPID
    
if [ "$SLAY_PPID" "exit" ] || [ "$SLAY_PPID" "" ]
    
then
        
exit
    
fi
    ps 
-p $SLAY_PPID grep -q $SLAY_PPID
    
if [ $? -eq 0 ]
    
then
        UNIX95
ps -o pid,user,state,comm -p $SLAY_PPID 
        
awk '$1 ~ /^[0-9]*$/ { print "The program " $4 " with PID " $1 " is being run by the user " $2 " and is currently in state " $3 }'
        
read -"Are you sure you want to kill PID $SLAY_PPID ? Y|N : " COMMIT_KILL
        
if [ "$COMMIT_KILL" "Y" ] || [ "$COMMIT_KILL" "y" ]
        
then
            kill 
-9 $SLAY_PPID
            
echo ""
            
read -"Killed PID $SLAY_PPID. Run again? Y/N : " GO_AGAIN
        
else
            
Z_Kill
        fi
    
else
        echo 
"Invalid PID. Try again."
        
echo ""
        
Z_Kill
    fi
}

function 
help
{
    echo 
"Usage: zombie_slayer [-h | help -d]"
    
echo "-d just display zombie processes and exit"
    
echo "-h | help : This help"
    
exit
}

if [ 
"$1" "-h" ] || [ "$1" "help" ]
then
    help
fi

if [ "$1" "-d" ]
then
    Z_Display
else
    
Z_Display
    Z_Kill
fi
if [ "$GO_AGAIN" "Y" ] || [ "$GO_AGAIN" "y" ]
then
    Z_Display
    Z_Kill
fi
echo "" 
__________________
Raj
Linux rulz.
I have never turned back in my life ; I shall not do so today.. haha
Reply With Quote
  #8 (permalink)  
Old 01-07-2009, 10:01 AM
kumarat9pm's Avatar
Senior Member
User
 
Join Date: Jun 2007
Location: Pune,MH,India
OS: RHEL,UBUNTU..
Posts: 447
Thanks: 20
Thanked 20 Times in 18 Posts
Rep Power: 5
kumarat9pm has a spectacular aura about kumarat9pm has a spectacular aura about kumarat9pm has a spectacular aura about
Send a message via Yahoo to kumarat9pm Send a message via Skype™ to kumarat9pm
Default Thanks raj for your time

thanks very much to all how helped me
Reply With Quote
Reply

Tags
kill , killall


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

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 Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
Process Killing -9 vs -10 and RAID 0+1 vs RAID 1+0 amsreeku Linux software 1 07-07-2008 03:44 PM
Killing of a process and send a mail if the process doesnot come up within 2 minutes Prince89 Shell scripting 1 11-02-2008 12:39 PM
process need more memory brijesh Linux software 3 13-12-2007 05:38 PM
httpd process vimalgoel Getting started tutorials 1 08-10-2007 12:40 AM
Help with killing sparky Shell scripting 5 14-04-2006 05:52 PM


All times are GMT +5.5. The time now is 10:02 AM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38