Passing a variable from awk to shell

31 views
Skip to first unread message

Dr Malcolm E.Parker

unread,
Nov 26, 1996, 3:00:00 AM11/26/96
to

Hi
I am pretty new to this game so apologies if the question seems simple.
I wish to run a script via cron, that searches for a process and then kills
it. I use the following code to search for the PID :-

ps -fu chs1mp | awk ' $8~/xclock/ && $1~/chs1mp/ { number=$2 ;
print number}' ;

and sure enough, the variable, number, is the PID I want. How do I then pass
this value to the Bourne shell to run a kill -9 on?

Thankyou in advance. Mal.

--
Dr Malcolm Parker | email: m.e.p...@surrey.ac.uk
Department of Chemistry, | www: http://www.chem.surrey.ac.uk/~chs1mp
University of Surrey, |
Guildford, Surrey. GU2 5XH. UK. | Tel: (01483) 300800x2580 Fax: (01483) 259514


Brian S Hiles

unread,
Nov 26, 1996, 3:00:00 AM11/26/96
to

Dr Malcolm E.Parker (chs...@surrey.ac.uk) wrote:
: I am pretty new to this game so apologies if the question seems simple.

: I wish to run a script via cron, that searches for a process and then kills
: it. I use the following code to search for the PID :-
: ps -fu chs1mp | awk ' $8~/xclock/ && $1~/chs1mp/ { number=$2 }
: and sure enough, the variable, number, is the PID I want. How do I then pass

: this value to the Bourne shell to run a kill -9 on?
: Thank you in advance. Mal.

ps -fu chs1mp |
awk '$1=="chs1mp" && $8~/^xclock/ { print $2 }' - |
xargs kill -9

-or-

ps -fu chs1mp |
awk '$1=="chs1mp" && $8~/^xclock/ { print $2 }' - |
while read pid
do kill -9 $pid
done

-or-

ps -fu chs1mp |
awk '$1=="chs1mp" && $8~/^xclock/ { print "kill -9 " $2 | "sh -s" }' -

comp.lang.awk may also be a newsgroup of interest to you.

-Brian
--
,---. ,---. ,---. ,---. ,---. ,---. ,---.
/ _ \ / _ \ / _ \ / _ \ / _ \ / _ \ / _ \
.' / \ `.' / mailto:bsh2...@challenger.atc.fhda.edu \ `.' / \ `.
__,' `.___,' `.___,' `.___,' `.___,' `.___,' `.___,' `.__

Cal Dunigan

unread,
Nov 26, 1996, 3:00:00 AM11/26/96
to

On 26 Nov 1996, Dr Malcolm E.Parker wrote:
> I wish to run a script via cron, that searches for a process and then kills
> it. I use the following code to search for the PID :-
>
> ps -fu chs1mp | awk ' $8~/xclock/ && $1~/chs1mp/ { number=$2 ;
> print number}' ;

You're almost there, now just use the shell's process substitution
feature:
kill -9 `ps -fu chs1mp | awk ' $8~/xclock/ && $1~/chs1mp/ {
print $2}'`

This *may* give you some trouble if there is more than one process
that meets the search criteria. If so just use printf.

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Cal Dunigan c...@ccdi.com There is something very wrong with
Consulting a world where Ken Thompson lives in
Modeling relative obscurity and Bill Gates
Training is a famous billionaire.
//////////////////////////////////////////////////////////////////////


Bill Marcum

unread,
Nov 27, 1996, 3:00:00 AM11/27/96
to

In article <57fa2n$n...@info-server.surrey.ac.uk>, Dr Malcolm E.Parker wrote:
>Hi

> I am pretty new to this game so apologies if the question seems simple.
>I wish to run a script via cron, that searches for a process and then kills
>it. I use the following code to search for the PID :-
>
>ps -fu chs1mp | awk ' $8~/xclock/ && $1~/chs1mp/ { number=$2 ;
> print number}' ;
>
>and sure enough, the variable, number, is the PID I want. How do I then pass
>this value to the Bourne shell to run a kill -9 on?
>
kill -9 `ps -fu chs1mp | awk ' $8~/xclock/ && $1~/chs1mp/ { number=$2 ;
print number}' `
Or

ps -fu chs1mp | awk ' $8~/xclock/ && $1~/chs1mp/ { number=$2 ;
system ("kill -9 " number)}'


--
Bill Marcum bma...@iglou.com
"You can pay Uncle Sam with the overtime
Is that all you get for your money?" --Billy Joel


Neil Moore

unread,
Nov 27, 1996, 3:00:00 AM11/27/96
to

chs...@surrey.ac.uk (Dr Malcolm E.Parker) writes:

>
> Hi
> I am pretty new to this game so apologies if the question seems simple.
> I wish to run a script via cron, that searches for a process and then kills
> it. I use the following code to search for the PID :-
>
> ps -fu chs1mp | awk ' $8~/xclock/ && $1~/chs1mp/ { number=$2 ;
> print number}' ;
>
> and sure enough, the variable, number, is the PID I want. How do I then pass
> this value to the Bourne shell to run a kill -9 on?
>

> Thankyou in advance. Mal.

kill -9 `ps -fu chs1mp | awk ' $8~/xclock/ && $1~/chs1mp/ { number=$2;
print number}' `

`` will convert a program's stdout into command-line parameters.

--
-Neil Moore
(finger amet...@170.180.106.55 for my Geek Code/Nethack Code)

Lawson Hanson

unread,
Nov 27, 1996, 3:00:00 AM11/27/96
to

chs...@surrey.ac.uk (Dr Malcolm E.Parker) writes:

>Hi
> I am pretty new to this game so apologies if the question seems simple.
>I wish to run a script via cron, that searches for a process and then kills
>it. I use the following code to search for the PID :-

>ps -fu chs1mp | awk ' $8~/xclock/ && $1~/chs1mp/ { number=$2 ;
> print number}' ;

>and sure enough, the variable, number, is the PID I want. How do I then pass
>this value to the Bourne shell to run a kill -9 on?

>Thankyou in advance. Mal.

>--

>Dr Malcolm Parker | email: m.e.p...@surrey.ac.uk
>Department of Chemistry, | www: http://www.chem.surrey.ac.uk/~chs1mp
>University of Surrey, |
>Guildford, Surrey. GU2 5XH. UK. | Tel: (01483) 300800x2580 Fax: (01483) 259514


One way (and I'm sure there are always dozens, because this is Unix),
would be to capture the output of your command into a variable:

PID=`ps -fu chs1mp | awk ' $8~/xclock/ && $1~/chs1mp/ { print $2 }'-`
^ ^
+----- NOTE the backward single quote characters, which --------+
cause the command to be evaluated, and the
result is assigned to shell variable PID.

Then if the result is non-null, use the result with "kill":

if [ -n "${PID}" ]; then
kill ${PID}
fi


I hope that helps.

Best regards,

Lawson Hanson

Dave Brown

unread,
Nov 27, 1996, 3:00:00 AM11/27/96
to

In article <57fa2n$n...@info-server.surrey.ac.uk>,
Dr Malcolm E.Parker <chs...@surrey.ac.uk> wrote:
: Hi

: I am pretty new to this game so apologies if the question seems simple.
: I wish to run a script via cron, that searches for a process and then kills
: it. I use the following code to search for the PID :-
:
: ps -fu chs1mp | awk ' $8~/xclock/ && $1~/chs1mp/ { number=$2 ;
: print number}' ;
:
: and sure enough, the variable, number, is the PID I want. How do I then pass
: this value to the Bourne shell to run a kill -9 on?

Other people have told you how to do what you want, so I'm going to
comment on something else.

If you have to use kill -9 to get rid of a process, then it's buggy and
you should fix it or remove it.

Kill -9 is only for killing badly-written code that refuses to
acknowledge the proper signals--INT, HUP, and TERM. Kill -9 doesn't
give programs a chance to remove any temporary files or do any other
kind of clean-up. It's a bad thing. Don't use it on a routine basis.
It's only for emergencies.

DO NOT USE KILL -9.

--Dave (who uses kill -9 as a sysadmin, because it puts up this wonderful
"Killed" message on the terminal, which is very good for dropping
hints to users as to what sort of programs I don't want them
to run. Otherwise, I don't use it)

Randal Schwartz

unread,
Nov 28, 1996, 3:00:00 AM11/28/96
to Bill Marcum

This month's Useless Use of Kill -9 award goes to...
>>>>> "Bill" == Bill Marcum <bma...@iglou1.iglou.com> writes:

Bill> kill -9 `ps -fu chs1mp | awk ' $8~/xclock/ && $1~/chs1mp/ { number=$2 ;
Bill> print number}' `
Bill> Or
Bill> ps -fu chs1mp | awk ' $8~/xclock/ && $1~/chs1mp/ { number=$2 ;
Bill> system ("kill -9 " number)}'

No no no. Don't use kill -9.

It doesn't give the process a chance to cleanly:

1) shut down socket connections

2) clean up temp files

3) inform its children that it is going away

4) reset its terminal characteristics

and so on and so on and so on.

Generally, send 15, and wait a second or two, and if that doesn't
work, send 2, and if that doesn't work, send 1. If that doesn't,
REMOVE THE BINARY because the program is badly behaved!

Don't use kill -9. Don't bring out the combine harvester just to tidy
up the flower pot.

Just another Useless Use of Usenet,

--
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying
Email: <mer...@stonehenge.com> Snail: (Call) PGP-Key: (finger mer...@ora.com)
Web: <A HREF="http://www.stonehenge.com/merlyn/">My Home Page!</A>
Quote: "I'm telling you, if I could have five lines in my .sig, I would!" -- me

Casper H.S. Dik - Network Security Engineer

unread,
Dec 2, 1996, 3:00:00 AM12/2/96
to

dagb...@calum.csclub.uwaterloo.ca (Dave Brown) writes:

>DO NOT USE KILL -9.

>--Dave (who uses kill -9 as a sysadmin, because it puts up this wonderful
> "Killed" message on the terminal, which is very good for dropping
> hints to users as to what sort of programs I don't want them
> to run. Otherwise, I don't use it)

Tsk, tsk, it's much more fun to do a "kill -BUS" or a "kill -SEGV".

See them firing up the debugger abd looking for the bug that doesn't
exist.

Casper
--
Expressed in this posting are my opinions. They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.

Goran Larsson

unread,
Dec 2, 1996, 3:00:00 AM12/2/96
to

In article <8cpw0y2g...@gadget.cscaper.com>,

Randal Schwartz <mer...@stonehenge.com> wrote:
| Generally, send 15, and wait a second or two, and if that doesn't
| work, send 2, and if that doesn't work, send 1. If that doesn't,
| REMOVE THE BINARY because the program is badly behaved!

It might also be hanging due to a badly written device driver in
the kernel, so the way to kill the program must be:

kill -15 (SIGTERM)
wait 2s
if not dead
kill -2 (SIGINT)
wait 2s
if not dead
kill -1 (SIGHUP)
wait 2s
if not dead
kill -9 (SIGKILL)
wait 1s
if not dead
fix device driver
else
remove program binary
end if
end if
end if
end if

--
Goran Larsson mailto:h...@approve.se
I was an atheist, http://home1.swipnet.se/%7Ew-12153/
until I found out I was God.

Dave Brown

unread,
Dec 2, 1996, 3:00:00 AM12/2/96
to

In article <casper.8...@uk-usenet.uk.sun.com>,
Casper H.S. Dik - Network Security Engineer <Caspe...@Holland.Sun.COM> wrote:

: dagb...@calum.csclub.uwaterloo.ca (Dave Brown) writes:
: >DO NOT USE KILL -9.
:
: >--Dave (who uses kill -9 as a sysadmin, because it puts up this wonderful
: > "Killed" message on the terminal, which is very good for dropping
: > hints to users as to what sort of programs I don't want them
: > to run. Otherwise, I don't use it)
:
: Tsk, tsk, it's much more fun to do a "kill -BUS" or a "kill -SEGV".
:
: See them firing up the debugger and looking for the bug that doesn't
: exist.

Oh, I've done that too--I had a user who insisted on running IRC bots
on my system. I eventually managed convinced him that they were far
too buggy to work properly. :-)

--Dave
--
http://www.csclub.uwaterloo.ca/~dagbrown/

Mike Miscevic

unread,
Dec 10, 1996, 3:00:00 AM12/10/96
to

Casper H.S. Dik - Network Security Engineer (Caspe...@Holland.Sun.COM) wrote:

: Tsk, tsk, it's much more fun to do a "kill -BUS" or a "kill -SEGV".
:

: See them firing up the debugger abd looking for the bug that doesn't
: exist.

Looks like someone's been BOFH'ing the users... :)

--
Mike Miscevic email: misc...@tcsi.com
TCSI Corporation phone: (408) 975-4707
150 Almaden Blvd., Suite 900 fax: (408) 975-2525
San Jose, California 95113 coffee: double/double

Reply all
Reply to author
Forward
0 new messages