1752794 Members
5976 Online
108789 Solutions
New Discussion юеВ

Re: Killing a process

 
SOLVED
Go to solution
TheJuiceman
Super Advisor

Killing a process

Hey gang,

OK...first of all, be gentle. You know how bad my script writing skills are. OK...here goes...

I need to write a script that will go out and find a process (a ps) and then kill it. I need something that will kill just a particular process (one particular return from a 'ps' command grepping out for a particular thing like "ps -ef|grep ") and I need something that will kill ALL of the processes that return from the ps.

Does anyone have any nice neat solutions for these two situations? Thanks again!!!
3 REPLIES 3
A. Clay Stephenson
Acclaimed Contributor

Re: Killing a process

First of all grep is a terrible tool to match processes; use the XPG4 ps behavior.

Now:
COMMAND="myproc" # you will probably pass this as a parameter
UNIX95=1 ps -C "${COMMAND}" -o pid='' | while read P
do
echo "PID = ${P}"
echo "Kill goes here but if I see a kill -9 you are dead"
echo "Start with kill -15 then -3 then -11 and then only if absolutely required kill -9"
# test to see if PID is still valid
kill -0 ${P}
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
echo "Process ${P} is still alive"
fi

done
--------------------------------
This should be more than sufficient for you to get a script working.
If it ain't broke, I can fix that.
Aussan
Respected Contributor

Re: Killing a process

kill $(UNIX95= ps -C -o pid=)


so for example

kill $(UNIX95= ps -C java -o pid=)

and that should kill any java process running
The tongue weighs practically nothing, but so few people can hold it
Bill Hassell
Honored Contributor
Solution

Re: Killing a process

Repeat the mantra:

ps and grep are not reliable!!!!

And the affirmation:

ps can find exactly what you want.

Seriously, combining grep and ps is always a disaster because you can't tell grep where to search. So start by NEVER combining grep and ps in a script and learning the many built-in search features of grep:

-u find all processes owned by a user
-p find a specific process by PID
-C find a process by name

There are a lot more but these are commonly overlooked. NOTE: these are *exact matches, not accidental matches to the wrong field. Consider:

Looking for process 123
ps -ef | grep 123
...finds bill123 (a user ID on the system) and also finds 1123 as well as 11234 and of course, and process with a command line containing 123. But ps -f -p 123 will find exactly one process -- no mistakes.

-C process_name needs some details. This is an exact match so while the processes sh ksh bash csh unhashdaemon are all the same to grep sh, -C sh is an exact match. However, -C (and -o) are part of the XPG4 implementation so a flag is needed to enable the options. This is the method:

UNIX95=1 ps -f -C sh

and now only sh processes will be found. Compare that to:

ps -ef | grep sh

Now for your task: You first must define the criteria for the processes to be found. It may be by user (-u) or by name (-C) or perhaps by some item on the command line. Then use the DIY option -o (Do-It-Yourself) to define the fields and order you want them. So you pick thie fields you want to see. For instance:

UNIX95=1 ps -C sh -o pid,ruser,arg

and you see just the 3 columns listed. Now for your kill script, once you have extensively tested the criteria, you can create the kill list a number of different ways:

Simplest:
K=$(UNIX95=1 ps -C myprog -o pid=)
kill -15 $K

Whatever you do, don't use kill -9. And you must protect the kill list from killing critical kernel processes. The best way is to run your script as an ordinary user.

And don't be tempted to set UNIX95 in your environment. It changes the behavior of a number of libraries and other prorams. Use it on the command line so it disappears automatically.


Bill Hassell, sysadmin