I wrote this today to kill orphan processes left by my very dirty chk_servs.ksh script that runs constantly in a VNC session using ssh-agent to ensure encrypted traffic. These orphaned procs were eating up my CPU.

# This script kills orphan processes for a particular user
if [[ $# < 1 || $# > 1 ]];then
        echo "Syntax : kill_orphans.ksh"
        exit 1
fi
 
user=$1
 
cat /etc/passwd | awk -F : '{print $1}' | grep $user >/dev/null
rc=$?
if [[ $rc > 0 ]];then
        echo "$user is not a valid user."
        exit 2
fi
 
ps -fu $user | awk '{print $3,$2}' | while read ppid pid
do
if [[ $ppid -eq 1 ]];then
        echo "Killing orphan process $pid..."
        kill $pid
fi
done
 
exit 0

About troyski

I'm a freelance UNIX engineer working in the UK. I'm married to Tina and between us we have six children. I'm a bit of an Apple fan boy, and all the Windows machines in the house are a thing of the past now.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Post navigation