Operating System - HP-UX
1753505 Members
4903 Online
108794 Solutions
New Discussion юеВ

Script to find process and kill

 

Script to find process and kill

I need a script that will do a grep of a process named "MIA.OFS" with the owner "bnk"
then kill the process.

Any help would be very greatful.
4 REPLIES 4
Pete Randall
Outstanding Contributor

Re: Script to find process and kill

In it's simplest form:

kill `ps -ef |grep "MIA.OFS" |grep bnk`
# that would be an orderly kill, if it doesn't work, try again, more forcefully
kill -9 `ps -ef |grep "MIA.OFS" |grep bnk`


Pete

Pete
James R. Ferguson
Acclaimed Contributor

Re: Script to find process and kill

Hi Chris:

Don't use 'grep' as this can lead to erroneous matches!

Do something like this to match 'MIA.OFS' exactly and only if the running user is "bnk":

# kill $(UNIX95= ps -C MIA.OFS -opid= -ouser=|awk '$2=="bnk" {print $1}')

Regards!

...JRF...

James R. Ferguson
Acclaimed Contributor

Re: Script to find process and kill

Hi (again) Chris:

If you are running 11.31, you could use 'pkill' as :

# pkill MIA.OFS -u bnk

In either case, there is an advantage to capturing the pid or pids that match. For example:

# PIDS=$(UNIX95= ps -C MIA.OFS -opid= -ouser=|awk '$2=="bnk" {print $1}')

...then allows a "kind" kill:

# kill ${PIDS}

...which can be escalated if necessary:

# if [ ! -z "${PIDS}" ]; then
> kill -1 ${PIDS}
> kill -15 ${PIDS} 2>/dev/null
> kill -9 ${PIDS} 2>/dev/null
> fi

Regards!

...JRF...

Re: Script to find process and kill

Hi James, Actually I made something similar to your first post ,BUT the 2nd one sounds more "attractive"