Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 9 Dec 2011 15:21:00 +0100
From:      Mark Martinec <Mark.Martinec+freebsd@ijs.si>
To:        freebsd-current@freebsd.org
Subject:   Re: Bug in Perl script
Message-ID:  <201112091521.00680.Mark.Martinec%2Bfreebsd@ijs.si>
In-Reply-To: <4EE1D951.2030509@gmail.com>
References:  <4EE1D951.2030509@gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
Alexander,

> I have a script that runs command tail with open descriptor.
> After 30 seconds, I close descriptor.  But descriptor not closed.
> When script is closed tail is present in ps aux.
>=20
> $log_file =3D path_to_log;
> eval {
>     local $SIG{ALRM} =3D sub { die; };
>     alarm (30);
>     open (LOG, "tail -F $log_file|") || die "=D0=A1an`t open logfile
> \"$log_file\"";
>     while (<LOG>) {
>         ***
>     }
>     alarm (0);
> };
> close (LOG);
> print ("Ok\n");
> exit(0);
>=20
> This code is good working in FreeBSD 8.2, but in FreeBSD 9.0 not working.

I don't see any difference in this respect between 8.2 and 9.0.

Even in 8.2 the spawned tail process stays alive until the moment
when it tries to write its next line to a pipe - only then it aborts
with 'Broken pipe'. On a busy log file this happens soon, on an
idling log file this may take forever.

You should abort the forked process when you no longer need it:

my $log_file =3D '/var/log/mail.log';
my $pid;
eval {
  local $SIG{ALRM} =3D sub { die "Time is up" };
  alarm(10);
  $pid =3D open(LOG, "tail -F $log_file|");
  $pid or die "Can't open logfile \"$log_file\": $!";
  while (<LOG>) {
    print "HERE: $_";
  }
  alarm (0);
  1;
} or do {
  alarm (0);
  print "Bail out: $@";
};
if ($pid) {
  print "Terminating process [$pid]\n";
  kill('TERM',$pid);
}
close(LOG);
print "Ok\n";


  Mark



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201112091521.00680.Mark.Martinec%2Bfreebsd>