Re: Catching print errors
- From: "Peter J. Holzer" <hjp-usenet2@xxxxxx>
- Date: Mon, 2 Apr 2007 13:22:04 +0200
On 2007-04-02 10:37, Jamie <nospam@xxxxxxxxxxxxx> wrote:
Kind of drastic measures, normally there isn't anything you can do about a full
disk. (which is why I would generally just ignore a problem like that, what
can I do about it?)
At least you can stop and exit with error message, alerting the user or
sysadmin that something is wrong, instead of blindly ploughing on and
possibly damaging other data.
Also, before exiting, you can maybe remove temporary files, thereby
freeing up space again. (So at least the other programs can continue to
work).
If this is a serious system, you might write all output to a temp file and use
rename() so that the update is atomic.
But that also only works if you detect that writing failed.
Consider:
my ($fh, '>', "$file.$$") or die;
while ($data = whatever()) {
print $fh $data;
}
close($fh);
rename("$file.$$", $file) or die;
If the disk fills up while you are writing to $fh, you will clobber
$file with an imcomplete new version. OTOH:
my ($fh, '>', "$file.$$") or die;
while ($data = whatever()) {
print $fh $data or die;
}
close($fh) or die;
rename("$file.$$", $file) or die;
Here the rename will only be reached if all the print calls and the
close call were successful. But it will leave a temporary file lying
around. So something like this would be better:
my ($fh, '>', "$file.$$") or die;
eval {
while ($data = whatever()) {
print $fh $data or die;
}
close($fh) or die;
rename("$file.$$", $file) or die;
};
if ($@) {
unlink("$file.$$");
die $@;
}
Note that in these examples I didn't actually test for the reason of the
failure. The disk might be full, or the user may have exceeded his
quota, or there might be an I/O error on the disk, etc. It doesn't
matter for the program, because in any case it cannot continue. It may
matter for the user, because he needs to do different things to remedy
the problem before starting the program again.
There is absolutely no way to "test" for a kill -9 and you could be stuck with
1/2 a file written to disk, if you're updating a file, this can have
consequences.
Yep, but the person who needs to consider these consequences is the
person who issues the kill -9, not the person who writes the script.
hp
--
_ | Peter J. Holzer | Blaming Perl for the inability of programmers
|_|_) | Sysadmin WSR | to write clearly is like blaming English for
| | | hjp@xxxxxx | the circumlocutions of bureaucrats.
__/ | http://www.hjp.at/ | -- Charlton Wilbur in clpm
.
- Follow-Ups:
- Re: Catching print errors
- From: Jamie
- Re: Catching print errors
- References:
- Catching print errors
- From: himanshu . garg
- Re: Catching print errors
- From: Jamie
- Catching print errors
- Prev by Date: Re: Problem in the Perl script
- Next by Date: profiling - analyse by time "on behalf of"?
- Previous by thread: Re: Catching print errors
- Next by thread: Re: Catching print errors
- Index(es):
Relevant Pages
|