Re: Shell script that finds a process by name, then kills (699 Views)
Reply
Advisor
Chris Campbell 77
Posts: 19
Registered: ‎03-12-2009
Message 1 of 17 (700 Views)
Accepted Solution

Shell script that finds a process by name, then kills

If if anyone can help me out it would be greatly helpful.

I need to create a cron that will do the following:

# ps -ef | grep GLB.JOB*
bnk 15855 1 0 Mar 10 ? 0:20 jsh -Jb -c EX EB.PHANTOM.PH MIA.
OFS.USER GLB.JOBBL
root 3208 23349 0 12:10:09 pts/tp 0:00 grep GLB.JOB*
#
Then to kill the PID in this case its 15855

Outstanding Contributor
Pete Randall
Posts: 16,205
Registered: ‎11-03-1996
Message 2 of 17 (700 Views)

Re: Shell script that finds a process by name, then kills

kill `ps -ef |grep GLB.JOB |grep -v grep |awk '{print $2}'`


Pete

Pete
Honored Contributor
Steven Schweda
Posts: 9,096
Registered: ‎02-23-2005
Message 3 of 17 (700 Views)

Re: Shell script that finds a process by name, then kills

A Forum search for keywords like:
ps grep kill
would find many examples, including some
cautions.
Advisor
Chris Campbell 77
Posts: 19
Registered: ‎03-12-2009
Message 4 of 17 (700 Views)

Re: Shell script that finds a process by name, then kills

Steven thanks for the heads up. I do see some risks using grep to find process name.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 5 of 17 (700 Views)

Re: Shell script that finds a process by name, then kills

Hi Chris:

If you are running 11.31, look at 'pkill'.

If you are not running 11.31, make sure you match the process _exactly_ by name:

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

...note that there is _whitespace_ following 'UNIX95=' so that this mode is armed _only_ for the duration of the command line.

Regards!

...JRF...
Acclaimed Contributor
Dennis Handly
Posts: 25,277
Registered: ‎03-06-2006
Message 6 of 17 (700 Views)

Re: Shell script that finds a process by name, then kills

>JRF: # kill $(UNIX95= ps -C GLBJOB -o pid=)

This won't work because GLB.JOB is a parm, not the name (jsh).

>note that there is _whitespace_ following 'UNIX95=

Instead of explaining this each time, I use:
UNIX95=EXTENDED_PS ps ...
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 7 of 17 (700 Views)

Re: Shell script that finds a process by name, then kills

Hi (again):

> Dennis: This won't work because GLB.JOB is a parm, not the name (jsh).

Ah, yes, so this should read:

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

...since the request is to "...finds a process by name, then kills...:

Regards!

...JRF...
Honored Contributor
Hein van den Heuvel
Posts: 6,588
Registered: ‎05-19-2003
Message 8 of 17 (700 Views)

Re: Shell script that finds a process by name, then kills

UNIX95 looking for process by name is the way to go.

But I have to admi that I do still too often search through ps -ef output.

If I do, then I prefer to have awk do all the work. I like to test with an echo statement first, then go for the kill:

$ ps -ef | awk '/GLB.JOB/ {system( "echo -9 " $2)}'

$ ps -ef | awk '/GLB.JOB/ {system( "kill -9 " $2)}'


fwiw,
Hein.
Honored Contributor
Steven Schweda
Posts: 9,096
Registered: ‎02-23-2005
Message 9 of 17 (700 Views)

Re: Shell script that finds a process by name, then kills

> [...] I like to test with an echo statement
> first, [...]

I'd be very reluctant to automate a job like
this fully, and then schedule it to run
periodically. I have an old, lame (ps+grep)
script ("/misc/kil") which displays the "ps"
line, and then asks the user for confirmation
before whacking anyone.

There are some good reasons to save a process
ID in a file somewhere when starting a job,
and then (after a quick validity check)
whacking that PID instead of trying to divine
the right PID later. Call me a coward.
Honored Contributor
dirk dierickx
Posts: 958
Registered: ‎12-20-1999
Message 10 of 17 (700 Views)

Re: Shell script that finds a process by name, then kills

pgrep and pkill commands will help. for this purpose pkill is a single command that basically does the ps, grep and kill commands all in one.
Honored Contributor
OldSchool
Posts: 3,372
Registered: ‎09-09-2004
Message 11 of 17 (699 Views)

Re: Shell script that finds a process by name, then kills

As noted above, the "UNIX95" switch won't work in this case, as he's looking at command parm, the actual job is "jsh".

I suspect that pgrep / pkill will have the same issue
Acclaimed Contributor
Dennis Handly
Posts: 25,277
Registered: ‎03-06-2006
Message 12 of 17 (699 Views)

Re: Shell script that finds a process by name, then kills

>OldSchool: the UNIX95 switch won't work in this case

That doesn't matter, you should always use UNIX95=EXTENDED_PS with ps(1). ;-)
My alias does that.
And if we really wanted to be picky, that should be fgrep or "GLB\.JOB".
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 13 of 17 (699 Views)

Re: Shell script that finds a process by name, then kills

Hi:

> OldSchool: the UNIX95 switch won't work in this case

I disagree if the process name is also known. If it isn't then I would agree.

Using the UNIX95 mode of 'ps' _will_ help _isolate_ the process by name. Then, a simple 'awk' can filter the match a bit more and issue the 'kill'.

> Dennis: you should always use UNIX95=EXTENDED_PS with ps(1). ;-)

Well, that's your preference and I will agree that it does eliminate ambiguity for many, but that is _not_ strictly necessary. Wriing "UNIX95= " with trailing whitespace is sufficient to set/define the environment. ;-)

So, in all, one might meld several thoughts together into something like:

# UNIX95= ps -C jsh -o pid= -o args=|awk '/GBL\.JOB.+/ {system("kill " $1)}'

...which looks for processes named 'jsh' with command arguments that contain "GBL.JOB*" [ which we might be able to anchor to the end of the string ] and then kills them. The "=" sign after the 'pid' and 'args' options suppresses the heading line so we don't have to deal with it.

Regards!

...JRF...

Acclaimed Contributor
Dennis Handly
Posts: 25,277
Registered: ‎03-06-2006
Message 14 of 17 (699 Views)

Re: Shell script that finds a process by name, then kills

>JRF: that's your preference

That's not my point. My point to OldSchool was that you should use UNIX95 with ps, even if it "won't work in this case".
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 15 of 17 (699 Views)

Re: Shell script that finds a process by name, then kills

Hi:

> Dennis: >JRF: that's your preference
That's not my point. My point to OldSchool was that you should use UNIX95 with ps, even if it "won't work in this case".

And I _absolutely_ agree. At the least, one can match a process name without undo fuss [ as for instance, when using 'grep |grep -v ' or other skulduggery to avoid matching yourself (the 'grep') ].

Interestingly, 'pgrep()' and 'pkill()' available in 11.31 avoid matching themselves.

With warm Regards!

...JRF...
Honored Contributor
OldSchool
Posts: 3,372
Registered: ‎09-09-2004
Message 16 of 17 (699 Views)

Re: Shell script that finds a process by name, then kills

I agree w/ both Dennis and JRF....

the point I was trying to make is that since the process is jsh, he can't use "GLB.JOB" for the process field, and for the same reason pkill probably won't succeed either....he'd have to look for the jsh process and then do additional filtering.

I should have been more clear....
Valued Contributor
rmueller58
Posts: 851
Registered: ‎02-19-2001
Message 17 of 17 (699 Views)

Re: Shell script that finds a process by name, then kills

ps -ef |grep -v -f /usr/local/bin/patt.1 |grep $1 > temp.1
kill -9 `awk '{print $2}' temp.1`
rm temp.1


where $1 = GLB.JOB*

The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.