1846741 Members
5371 Online
110256 Solutions
New Discussion

Simple Script Help

 
SOLVED
Go to solution
Nobody's Hero
Valued Contributor

Simple Script Help

I have some processes that I want to kill on a daily basis, like:
#ps -ef | grep exec

root 25098 1 0 01:00:01 ? 00:00:00 /usr/lib/mail/execmail root

How can I pull out the pid and kill it?

Also I want to kill all processes by a specific user named mmdf, like;

# ps -fu mmdf
mmdf 25057 265 0 01:00:00 ? 00:00:00 sh -c /usr/mmdf/bin/cleane
mmdf 16861 25063 4 07:11:50 ? 00:01:02 submit
mmdf 25063 25057 1 01:00:00 ? 00:22:28 /usr/mmdf/bin/cleanque



UNIX IS GOOD
9 REPLIES 9
James R. Ferguson
Acclaimed Contributor
Solution

Re: Simple Script Help

Hi Robert:

Use the UNIX95 (XPG4) form of 'ps' to find your processes by name:

Suppose I want to look for a 'sleep' process owned by root:

# PID=`UNIX95= ps -e -o pid,user,comm|awk '$2~/root/ && $3~/sleep/ {print}'`

Then:

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

The above does more and more brutal kills. If the process dies along the way, the redirection of STDERR eliminates seeing the "non-existent process" message.

The last point is never to do a 'kill -9' except as a last restort. A 'kill -9' cannot be trapped by a process leaving it no opportunity to free shared memory and/or remove any temporary files.

Regards!

...JRF...
Steven E. Protter
Exalted Contributor

Re: Simple Script Help

Shalom Robert,

A few ways to go. hopefullly there is something unique about the process.

Lets say we want to kill a process named execmail

PID=$(ps -ef | grep execmail | grep -v grep| awk '{print $2}'

kill $PID

SEP

You may need
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Pete Randall
Outstanding Contributor

Re: Simple Script Help

#ps -ef | grep exec | awk '{ print $2 }' > /tmp/kill_list
#for i in `cat /tmp/kill_list`
do
kill $i
sleep 1
kill -9 $i
done

Same thing for a particular user.


Pete

Pete
James R. Ferguson
Acclaimed Contributor

Re: Simple Script Help

Hi (again) Robert:

Also, note carefully that I wrote:

# UNIX95= ps ...

There is a blank (space) character after the UNIX95= but *before* the 'ps'. This limits the setting of the UNIX95 (XPG4) behavior to the command line on which it appears. You don't want this setting to influence other commands without your explicit choice!

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Simple Script Help

Hi Robert:

One correction. I should have said "... print $1)'` as:

# # PID=`UNIX95= ps -e -o pid,user,comm|awk '$2~/root/ && $3~/sleep/ {print $1}'`

This builds a list of 0 to n process IDs and allows seeking and killing in one pass.

Regards!

...JRF...



Steven E. Protter
Exalted Contributor

Re: Simple Script Help

note my latest typo


PID=$(ps -ef | grep execmail | grep -v grep| awk '{print $2}'

Needs to be


PID=$(ps -ef | grep execmail | grep -v grep| awk '{print $2}')

A minor but potentially disasterous problem.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Yogeeraj_1
Honored Contributor

Re: Simple Script Help

hi robert!

see also the thread for the question that i had recently posted:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=969402


hope this helps too!

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Bill Hassell
Honored Contributor

Re: Simple Script Help

It is VERY important to *not* use grep or awk to find the processes! The reason is that grep and awk will not find just the process name, they will match user and group ID's, pathnames (ie, directories) and processes with the string somewhere in the name.

The good news is that ps can perform an exact match for a process name, user and group ID, and eliminate potentially critical mistakes. For your first example:

UNIX95=1 ps -C execmail -o pid,ruser,args

will only find processes exactly named execmail (not rexecmail22 and other variations). Using the -o options helps to limit the required items of data. You can eliminate the title line for scripting by setting the label to null (-o pid= -o ruser= -o args=)


Bill Hassell, sysadmin
Steven E. Protter
Exalted Contributor

Re: Simple Script Help

BH, my emails to you bounce.

I think grep can be made safe but...arguing with BH is a sure way to lose an argument.

searchstring=$1
# make first input parameter the search string.
PID=$(UNIX95=1 ps -C $searchstring -o pid= -o ruser= -o args= | awk '{print $1}')

kill $PID

I also recommend not allow search parameters like root.

Safer.

BH approved?

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com