[sf-perl] Log stomping

David Alban extasia at extasia.org
Wed Aug 24 10:06:26 PDT 2005


On 8/24/05, Bill Moseley <moseley at hank.org> wrote:
> This really sounds more like an OS question, so I'll be guessing from
> this point on.  This makes me realize I don't understand shell redirection
> very well.

And it didn't occur to me that stomping would occur in solaris 8 sh,
but not in solaris 8 ksh, or gnu bash. :-)

> Stdout is buffered by perl -- seems possible that Perl is closing your
> lock file before flushing stdout.  That's basically the same thing as
> (the commonly done) explicitly unlocking before closing.

If you do:

  $ my-perl-prog > somefile

You'll obviously get the stdout of my-perl-prog in somefile.  What if
you mistype:

  $ my-pearl-prog > somefile

You'll get some kind of file or command not found error, but notice
that regardless of whether the shell was able to execute your command,
it will either create and empty somefile (if it didn't exist) or
truncate to zero length any existing somefile.  I believe the shell
does this even before attempting to find and run the given command.

My theory in my original post is still my theory, at least for solaris
8 Bourne shell:  that as soon as multiple cron jobs start at the same
instant, and appending to the same log file, they will immediately try
to determine the position of EOF for the log file, whether or not they
have anything yet to append.  And when one of the jobs does append,
the other jobs have not updated their position in the file, so they
may clobber what was written by a previous job.  Perhaps ksh and bash
are smarter than solaris 8 sh in this regard.  I don't know how, but
they seem to do The Right Thing.

> Why not flock the log file and write directly to that?

That's fine for stuff logged by my program, but I also want stderr to
be appended to the log file.

David

P.S.  Hope folks didn't mind a slightly less perl-ish, slightly more
shell-ish thread...
-- 
Live in a world of your own, but always welcome visitors.


More information about the SanFrancisco-pm mailing list