1833696 Members
3616 Online
110062 Solutions
New Discussion

kill shell script

 
SOLVED
Go to solution
Brent W. Moll
Advisor

kill shell script

I need to determine the PID for a process in a shell script, then kill the process.

Example:

[sdatpp01][/home/root] #ps -ef | grep dsmc
root 12117 1 13 Nov 6 ? 40:01 dsmc sched
root 27781 23503 2 10:13:11 pts/0 0:00 grep dsmc
[sdatpp01][/home/root]

In this case I need to put the PID 12117 in a variable PID$

how do I do this in a script ?
10 REPLIES 10
Steven E. Protter
Exalted Contributor
Solution

Re: kill shell script

Here is a script. It might work off the shelf, or require minor modification.

Attached.

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
Mark Greene_1
Honored Contributor

Re: kill shell script

In sh and ksh, $PID is the script process, $PPID is the process that ran the script.

If you are looking to extract a PID from a ps listing, try this:

_PID=`ps -ef|grep $_STRING|tr -s " "|cut -d" " -f3`

mark
the future will be a lot like now, only later
Pete Randall
Outstanding Contributor

Re: kill shell script

Brent,

Here's a sample of one of our kill scripts:

for PID in `ps -ef |grep web_command | grep -v grep |awk '{ print $2 }'`
do
kill -9 $PID
echo "web_command.ksh stopped"
done


Note that the usage of kill -9 is not recommended because it does not allow for the normal housekeeping when a process terminates and may leave memory allocated, etc, etc.


Pete

Pete
Graham Cameron_1
Honored Contributor

Re: kill shell script

Pete you are so brutal.

Round here we like to give things chance to die first.

Instead of
kill -9 ${PID}

we would have
for sig in 1 2 3 15 9
do
kill -${sig} {PID} 2>/dev/null
done

-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Michael Schulte zur Sur
Honored Contributor

Re: kill shell script

Hi Brent,

you need field #2 in the ps output.

ps -ef | grep jobname | grep -v grep | awk '{print $2}'

field #3 is the parent process.

Michael
Pete Randall
Outstanding Contributor

Re: kill shell script

Graham,

True, it's brutal but it's a shutdown script for an unimportant process (and I did note that it's not recommended). Since we're shutting down anyway, why would I care? In other circumstances you method would be much more appropriate.


Pete

Pete
Michael Schulte zur Sur
Honored Contributor

Re: kill shell script

Hi Pete,

What do you think, if the process listens to kill -3, should be better, right?
Well, early bird catches the worm. I was thinking to long. ;-) grumble mumble..

greetings,

Michael
Pete Randall
Outstanding Contributor

Re: kill shell script

Michael,

What I think is that when I'm shutting down, I don't care what happens to the process. I don't care if it shuts down gracefully and release any memory regions and cleans up after itself. I'm shutting down, not worrying about etiquette or hurting a process's feelings. I want the damn thing to die and die now.

Other processes (like the database, for example) I obviously want to shut down normally but trivial ones like this I don't care about. The whole point here was to answer Brent's original question about getting the PID.

End of subject - please.


Pete

Pete
Michael Schulte zur Sur
Honored Contributor

Re: kill shell script

Hi Pete,

I totally agree with you. It doesnt matter. I am talking about ending a process which is running in the backgound and stopping it with -9 might hurt in cases like disk defrag. That was not meant to critisize you in any way. ;-)

Michael
rmueller58
Valued Contributor

Re: kill shell script

--------------------------------------------------------------------------------
If you want to kill all processes related to a specific user we set up the following..

# killit /usr/local/bin/killit
# ESU#3 - Systems and Networking
# rcm(10/03/2001)
# this script was written to kill hung users
#
#==============================================
# BEGIN SCRIPT
ps -ef |grep -v -f /usr/local/bin/patt.1 |grep $1 > temp.1
kill -9 `awk '{print $2}' temp.1`
rm temp.1
# END SCRIPT
#==============================================

note ** /usr/local/bin/patt.1 file contains patterns that you want to avoid. it allows you to exclude items that you want to avoid killing..