|
Posted by smallpond on April 24, 2008, 11:06 am
Please log in for more thread options On Apr 24, 10:46 am, xhos...@gmail.com wrote:
> > I have a script that tails a file and on a SIGTERM I want it to exit.
> > Sounds simple enough. It traps the signal, executes an 'exit', and then
> > hangs! The real odd thing is one of the modules I'm 'using' is
> > Time::Local. If I comment it out the script exits on SIGTERM and if it
> > leave it in it doesn't. The only way I was able to figure this out was
> > methodically removing pieces of code until it worked correctly and as
> > you might guess, the last thing I thought of was removing some of the
> > 'use' statements. Anyhow this leads to a few questions:
>
> > - does anyone have a clue what this is breaking things?
> > - how would you have tracked down this problem with the Time module
>
> The hang is because upon the clean up done at exit, the program is calling
> waitpid to wait for the "tail -f ..." process.
>
> Why Time::Local causes it to decide to clean up this process while
> commenting out the Time::Local causes it to not clean it up is still a
> mystery to me. The two behaviors are inconsistent, but it isn't clear to me
> which one, if any, is the "more correct" behavior. The one without the
> Time::Local silently leaves tail -f processes hanging around the system's
> process table, which certainly isn't desirable.
>
> > - how can I make this work correctly AND use Time?
>
> Kill the tail, as shown below:
>
>
>
> > Anyhow, here's a pretty small piece of code that fails! Just comment
> > out the 'use Time' and it works...
>
> > #!/usr/bin/perl -w
>
> > use strict;
> > use Time::Local;
>
> > print "PID: $$\n";
>
> my $kid_pid; #needs to be in scope of both open and sig_handler
>
> > $SIG=\&sigTerm; # default kill command
> > while(1) {
> > open TAIL, "tail -f /var/log/aggregate|" or die;
>
> $kid_pid=open TAIL, "tail -f /var/log/aggregate|" or die;
>
> > while (my $line=<TAIL>) {
> > my $x=1;
> > }
> > }
>
> > sub sigTerm {
> > print "SIGTERM\n";
>
> kill 15, $kid_pid;
>
> > exit(1);
> > }
>
> Xho
>
It might be better not to start a child process in
the first place. How to tail a file in perl is a FAQ.
perldoc -f tail
|