Simple Unix Question, hopefully

Simple Unix Question, hopefully

Post by Maurice Samue » Sat, 28 Jun 1997 04:00:00



Okay here's the problem:

I am writing a Bourne shell script and basically the purpose of the
program is to:
(1) run a program
(2) redirect the programs output to a file
(3) kill the program when an identifying sequence of characters is found
    in the file
here's an example:
say I wanted to run the top command  and direct it's output to a file called
top_mmddyy.log
and then use grep to find a particular user's name and then exit top.

now I thought it would be a good idea to use the following command line:

top >> top_mmddyy.log &

I figure that if I run this in the background then I can use
kill -9 %top to kill top

but that doesn't seem to work. when i try to use jobs to list the
background processes (i'm on an HP) somehow /stderr can't be read and I am
forced to type ^J stty sane ^J to reset my command line. I guess what I
really want to know is if anyone has any suggestions as to how I should
put the job in the background and how should I go about killing it.
thanks in advance -maurice

email is probably the best way to get in touch with me:

 
 
 

Simple Unix Question, hopefully

Post by mazier » Sun, 29 Jun 1997 04:00:00



> Okay here's the problem:

> I am writing a Bourne shell script and basically the purpose of the
> program is to:
> (1) run a program
> (2) redirect the programs output to a file
> (3) kill the program when an identifying sequence of characters is found
>     in the file
> here's an example:
> say I wanted to run the top command  and direct it's output to a file called
> top_mmddyy.log
> and then use grep to find a particular user's name and then exit top.

> now I thought it would be a good idea to use the following command line:

> top >> top_mmddyy.log &

> I figure that if I run this in the background then I can use
> kill -9 %top to kill top

> but that doesn't seem to work. when i try to use jobs to list the
> background processes (i'm on an HP) somehow /stderr can't be read and I am
> forced to type ^J stty sane ^J to reset my command line. I guess what I
> really want to know is if anyone has any suggestions as to how I should
> put the job in the background and how should I go about killing it.
> thanks in advance -maurice

> email is probably the best way to get in touch with me:


Remember that in Bourne (and Korn) shell "$!" gives you the PID of the
LAST background process (dead or alive).
I presume that what you need here is to store "$!" in a local variable
immediately after :
top >> top_mmddyy.log &

Therefore you can kill the process whenever you want just by invoking
its PID.

Here is a sample program you can run to check if it's the behaviour
you're looking for. It runs a dtterm then a hpterm (both in background)
and kills the dtterm. Outputs are redirected to bkgjobs.out just to
let you see what happens.

#!/bin/sh
dtterm&
dtterm_pid=$!
echo "Launching dtterm" > bkgjob.out
ps >> bkgjob.out
echo "Last pid = $!" >> bkgjob.out
echo "dtterm_pid = $dtterm_pid" >> bkgjob.out
echo >> bkgjob.out

hpterm&
hpterm_pid=$!
echo "Launching hpterm" >> bkgjob.out
ps >> bkgjob.out
echo "Last pid = $!" >> bkgjob.out
echo "hpterm_pid = $hpterm_pid" >> bkgjob.out
echo >> bkgjob.out

echo "Killing $dtterm_pid ..." >> bkgjob.out
sleep 5
kill $dtterm_pid
ps >> bkgjob.out

Maz.

 
 
 

1. Unix-based PH server (hopefully simple) ?


what do you mean by simple ?

there's qi server and ph client that work marvelously together on UNIX machine.


if you happen to be interested in a WWW client for qi/ph such as the one
that can be seen at http://www.middlebury.edu/cgi-bin/WebPH.pl contact

2. migrating linux system to new hardware/new harddrive

3. Simple (hopefully) question

4. RedHAT 5.1

5. Two simple questions - hopefully

6. lpd does not filter bofore spooling to remote. Help

7. simple bridge question, hopefully

8. Stupid Question

9. Hopefully simple sendmail question

10. two (hopefully) simple questions: root's shell and ipf

11. Hopefully a simple question.

12. Hopefully a simple login question

13. Hopefully Simple Questions