Operating System - HP-UX
1847152 Members
6485 Online
110263 Solutions
New Discussion

Re: killing process pid with a script ???

 
SOLVED
Go to solution
someone_4
Honored Contributor

killing process pid with a script ???

How would I go about writing a script that will kill a PID. The issue is the PID is always different so when I write this script is there a way it can pick out what the pid is if I write the name of the process ? Or is there any other suggestions ?
8 REPLIES 8
Maureen Gunkel
Trusted Contributor
Solution

Re: killing process pid with a script ???

Richard,
There are a couple of ways you can do it, and I have done at least 2 of them! One way is to store the pid in a temp file on start-up of the process, then when you're ready to kill it, you just grap the contents of that temp file. Another way is using ps, grepping for what you want and don't want, and then using awk to strip out just the PPID (parent process id). If you need samples, let me know.
HTH,
Mo
No matter where you go, there you are.
someone_4
Honored Contributor

Re: killing process pid with a script ???

yes an example would be great ..
thanks
Josef Nordtome
Advisor

Re: killing process pid with a script ???

Richard,

This is actually a very simple process. Attached is a script that will look for "program_name" and kill it's process id. If this is running from something like cron, you might want to source the /etc/profile in the script to set up the correct environment.

Josef
Life is something to do when you can't get to sleep.
Maureen Gunkel
Trusted Contributor

Re: killing process pid with a script ???

Here you go - - -
No matter where you go, there you are.
CHRIS_ANORUO
Honored Contributor

Re: killing process pid with a script ???

Hi Richard,
Try this script:
ee=`ps -e | grep process_name |cut -c2-6`
kill -9 $ee
If it is a rogue process try:
ee=`ps -e|grep process_name | grep "?" |cut -c2-6`
kill -9 $ee
When We Seek To Discover The Best In Others, We Somehow Bring Out The Best In Ourselves.
Bill Hassell
Honored Contributor

Re: killing process pid with a script ???

It's easy to grep through ps output and locate processes with a string but it is very UNSAFE. Be sure to limit the output of ps to just the PID, PPID (if needed) and the command (not the command line, aka, arg in ps). Then it's important to get rid of the grep command itself since grep is also a process and it will often find itself.

Another problem is that the program you are looking for may have a short name which is part of another program's name as in: myprocess and myprocess2 and myprocess3. If you kill all processes called "myprocess", you'll also kill myprocess1 and myprocess2.

It's also important to handle the multiple occurances of the same program...do you want to kill avery occurance or just the first one you find or the one with the oldest start time...?

The example script that Maureen posted shows a great feature in ps, the -o option. But this will not work (see than man page) unless you set the ENV variable called UNIX95. Since UNIX95 set in the environment also affects *many* other processes *and* libraries, it's not advisable to globally set this variable. Instead, set it just for the ps command as in:

UNIX95= ps -e -o 'pid,ppid,comm'

It's a good idea to put the word: echo in front of the kill commands while you are testing the script. That way, you'll see what the script will do before it's too late. Be sure to trap any process owned by root and don't kill it!


Bill Hassell, sysadmin
Maureen Gunkel
Trusted Contributor

Re: killing process pid with a script ???

Bill-
Good points! The scripts I submitted were actually for a Sun box, and I forgot that I had to set the UNIX95 variable first for HP. Also a great point on the 'echo kill', that's what I always do while testing!
Mo
No matter where you go, there you are.
someone_4
Honored Contributor

Re: killing process pid with a script ???

all of your answers were great !
I want to thank everyone for your help..
I tried to give everyone a 10 but it didnt let me.
thanks allot guys.
case closed.