Operating System - HP-UX
1823366 Members
2831 Online
109654 Solutions
New Discussion юеВ

Killing processes automatically.

 
SOLVED
Go to solution
santosh jha
Frequent Advisor

Killing processes automatically.

I want to kill weblogic process through a script.

i use the command

"kill -9 `ps -e |grep weblogic.server |cut -c 3-6`"

But there is chance that the number of digits in the pid may increase and than the script will not work.

Please help

Santosh jha
9 REPLIES 9
Doug O'Leary
Honored Contributor
Solution

Re: Killing processes automatically.

Hey;

Automatically killing processes is generally a bad idea. If, for some reason, your script identifies process 1, for instance, your system is history.

That being said:

ps -ef | grep [w]eblogic_serve | awk '{print $2}' | xargs -i kill {}

is the syntax I'd use if I wanted to do something as dangerous...

Doug

------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
Pete Randall
Outstanding Contributor

Re: Killing processes automatically.

Use awk rather than cut:

"kill -9 `ps -e |grep weblogic.server | awk '{ print $2 }'`"


Pete

Pete
santosh jha
Frequent Advisor

Re: Killing processes automatically.

Thnaks a lot guys .
Hakan Aribas
Valued Contributor

Re: Killing processes automatically.

You can use the following script to kill processes.

Usage:
killall

Example:
killall weblogic
---------------------

#!/usr/bin/sh

PS=/usr/bin/ps
AWK=/usr/bin/awk
GREP=/usr/bin/grep
KILL=/usr/bin/kill

if [ $# -ne 1 ]
then
echo Kullanim : ./killall proses_adi
exit
fi

for pid in `$PS -e | $GREP $1 | $GREP -v grep | $AWK ' { print $1 }'`
do
$KILL -9 $pid
echo $pid $1 killed
done
-------------
Geoff Wild
Honored Contributor

Re: Killing processes automatically.

Don't use -9 as you can windup leaving zombies and/or shared memory not cleared, etc....

kill -18 is better or just the default (15)...

What I used to do for example - on zombies:

logfile=/tmp/killzombies.log
if [ -f $logfile ];
then
rm $logfile
touch $logfile
else
touch $logfile
fi


Rgds...Geoff

for i in `ps -el|grep -v SZ|grep Z|awk '{print $5}'`
do
ps -ef|grep -v grep|grep ${i} >>$logfile 2>&1
mailx -s "Check Zombie(s)" me@mydomain.com <$logfile
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Bill Hassell
Honored Contributor

Re: Killing processes automatically.

Not a good design at all. kill -9 is the first problem. NEVER use kill -9 until you have tried kill -15 and perhaps kill -1. The reason is that kill -9 prevents the program code from cleaning up open files and logs and releasing shared memory, semaphores and memory mapped files. I would suggest that your script try each kill signal and then issue a warning when resorting to kill -9.

The secon d is ps and grep--a deadly combination. grep doesn't care what it finds on the ps line. The string "weblogic.server" will appear is the ps listing as part of the grep string or it may appear as a parameter given to some program. A worse case is where you grep for weblogic and there are users with that username, there are programs like weblogic.control, etc, all will be found and blindly killed by such a script.

Now true, you are using ps -e rather than -ef which drops the command line arguments, but it is always recommeded to use ps to locate your process by name, not grep. To show you how bad this can be, try these two commands:

ps -ef | grep sh
UNIX95= ps -C sh

The first will find critical system processes like unhashdaemon (don't kill that one!) while the second only find sh (and not ksh or csh, etc)

Here is a general method where the program name is assigned to a variable, and it exactly located with ps -C (note that -c -H and -o are enhanced ps commands and need the UNIX95 variable temporarily set to activate these options)

#!/usr/bin/sh
set -u
export PATH=/usr/bin
PROGNAME=sleep
PROGLIST=$(UNIX95= ps -C $PROGNAME -o pid=)

if [ "$PROGLIST" = "" ]
then
echo "\n$PROGNAME not running\n"
exit 1
fi

for PIDNUM in $PROGLIST
do
echo "\nKilling PID $PIDNUM:"
ps -fp $PIDNUM
kill -15 $PIDNUM
sleep 1
ps -fp $PIDNUM > /dev/null
if [ $? -eq 0 ]
then
kill -1 $PIDNUM
sleep 1
ps -fp $PIDNUM > /dev/null
if [ $? -eq 0 ]
then
kill -9 $PIDNUM
sleep 1
echo "(kill -9 $PIDNUM required)
ps -fp $PIDNUM > /dev/null
if [ $? -eq 0 ]
then
echo "\nWARNING: $PIDNUM cannot be killed\n"
fi
fi
fi
done

Now in this example, I've set PROGNAME to "sleep" so you can test it. It will handle zero or more copies of the same program name, first trying to terminate with SIGTERM (-15), then SIGHUP (-1) and finally SIGKILL (-9) with a warning. And if the process is still not dead, it is hung on I/O and can never be killed.


Bill Hassell, sysadmin
Bill Hassell
Honored Contributor

Re: Killing processes automatically.

Ops, one typo:

echo "(kill -9 $PIDNUM required)

is missing " at the end, should look like this:

echo "(kill -9 $PIDNUM required)"


Bill Hassell, sysadmin
santosh jha
Frequent Advisor

Re: Killing processes automatically.

Thanks a lot Bill.I will sure follow your instructions.

Santosh Jha
Rory R Hammond
Trusted Contributor

Re: Killing processes automatically.

Instead of relying on ps,

Try

fuser -k programname


If Weblogic as startup script use the traditional shutdown and startup
/sbin/init.d/weblogic restart

or

/sbin/init.d/weblogic stop
/sbin/init.d/weblogic start

Rory
There are a 100 ways to do things and 97 of them are right