1834928 Members
2527 Online
110071 Solutions
New Discussion

ksh scripting

 
SOLVED
Go to solution
Klaas D. Eenkhoorn
Regular Advisor

ksh scripting

Dear all,

I have a question wich breakes my mind.

I have a script monitoring a log file for specific messages.
I use the construction:

tail -f $logfile | while read line
do
case $line in
ORA-*)
echo "Fout in logfile: $line
;;
esac
done

This script, written in KSH will run until the system is stopped or until i kill the script.
If i do this with kill -15 PID (of the script)
Tail stays behind in the process table with a parent process of 1

How do i stop this process together with my script ?

Kl@@s
11 REPLIES 11
Mark Grant
Honored Contributor
Solution

Re: ksh scripting

Kill the PID of the "tail" instead. This will also cause the script to finish.
Never preceed any demonstration with anything more predictive than "watch this"
Kent Ostby
Honored Contributor

Re: ksh scripting

From the man page on the -f option:

(tail enters
an endless loop wherein it sleeps for one second
then attempts to read and copy further records
from the input file)

So thats why there is a problem. I'm not sure of a solution yet :-) I'll continue looking.
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Jean-Luc Oudart
Honored Contributor

Re: ksh scripting

The parent PID of tail is your script.
you shouild be able to find both PID and kill them all in one go
kill -15 PID1 PID2

Regards,
Jean-Luc
fiat lux
Klaas D. Eenkhoorn
Regular Advisor

Re: ksh scripting

Is it realy that simpel . . . :`-(
Well it's friday my mind seems to be on leaf allready.

Kl@@S
MarkSyder
Honored Contributor

Re: ksh scripting

Have you tried kill -9?

Mark Syder (like the drink but spelt different)
The triumph of evil requires only that good men do nothing
Michael Schulte zur Sur
Honored Contributor

Re: ksh scripting

Hi,

the script is run by who? By an interactive shell, with nohup or what? When you run it with nohup, you will have to kill the tail. Of course you can allways kill the shell too.

greetings,

Michael
Paula J Frazer-Campbell
Honored Contributor

Re: ksh scripting

Mark

-9 ??

Sledgehammer to catch a butterfly,
Use kill -9 as the very very last resort.

Paula
If you can spell SysAdmin then you is one - anon
Mark Greene_1
Honored Contributor

Re: ksh scripting

Klaas,

As the other Mark G wrote, kill the "tail" process. It should go away with just a "kill". If you have to use a -9, you have other problems; however, your script is hardly that complex so you ought not have to resort to the -9.

mark
the future will be a lot like now, only later
Rodney Hills
Honored Contributor

Re: ksh scripting

A little know fact about the "kill" command is that if you put a "-" in front of the pid, it will kill all processes that in that "process group".

$ ps -ef | grep tail
rhills 6725 6723 0 11:58:12 pts/tTb 0:00 tail -f /var/adm/syslog/syslog.log
rhills 6730 5560 2 11:58:18 pts/tt 0:00 grep tail
pegasus rhills $ kill -15 -6723
pegasus rhills $ ps -ef | grep tail
rhills 6744 5560 0 11:58:32 pts/tt 0:00 grep tail

I killed the pid 6723 (ksh parent of tail child) and it not only killed the ksh script, but also the tail.

Another alternative is to put a "trap" in your script, that when the ksh script is killed, it then kills the tail command.

HTH

-- Rod Hills
There be dragons...
Laurent Menase
Honored Contributor

Re: ksh scripting

kill -15 -PID

with -PID you send the signal to all the proces s group.

this ksh is not interactif so the other are run in its process group.

Brian Markus
Valued Contributor

Re: ksh scripting

I use this:

export JOBNUM=$$

$$ is the PID of the current script. You can store that and kill it later when ever you're ready.

Hope this helps

-Brian.
When a sys-admin say's maybe, they don't mean 'yes'!