parallel: auto-unlink tmp-files so if we forget to unlink it is OK.

This commit is contained in:
Ole Tange 2013-01-08 20:41:53 +01:00
parent 2c641b47d0
commit 05330bf8ea

View file

@ -181,7 +181,7 @@ if($opt::nonall or $opt::onall) {
# Copy all @fhlist into tempfiles # Copy all @fhlist into tempfiles
my @argfiles = (); my @argfiles = ();
for my $fh (@fhlist) { for my $fh (@fhlist) {
my ($outfh,$name) = ::tempfile(SUFFIX => ".all"); my ($outfh, $name) = ::tempfile(SUFFIX => ".all", UNLINK => 1);
print $outfh (<$fh>); print $outfh (<$fh>);
close $outfh; close $outfh;
push @argfiles, $name; push @argfiles, $name;
@ -1051,7 +1051,7 @@ sub read_args_from_command_line {
# Group of arguments on the command line. # Group of arguments on the command line.
# Put them into a file. # Put them into a file.
# Create argfile # Create argfile
my ($outfh,$name) = ::tempfile(SUFFIX => ".arg"); my ($outfh,$name) = ::tempfile(SUFFIX => ".arg", UNLINK => 1);
unlink($name); unlink($name);
# Put args into argfile # Put args into argfile
print $outfh map { $_,$/ } @group; print $outfh map { $_,$/ } @group;
@ -2383,7 +2383,7 @@ sub swap_activity {
# As the command can take long to run if run remote # As the command can take long to run if run remote
# save it to a tmp file before moving it to the correct file # save it to a tmp file before moving it to the correct file
my $file = $self->{'swap_activity_file'}; my $file = $self->{'swap_activity_file'};
my $tmpfile = $self->{'swap_activity_file'}.$$; my ($dummy_fh, $tmpfile) = ::tempfile(SUFFIX => ".swp", UNLINK => 1);
qx{ ($swap_activity > $tmpfile; mv $tmpfile $file) & }; qx{ ($swap_activity > $tmpfile; mv $tmpfile $file) & };
} }
return $self->{'swap_activity'}; return $self->{'swap_activity'};
@ -2466,7 +2466,7 @@ sub loadavg {
# As the command can take long to run if run remote # As the command can take long to run if run remote
# save it to a tmp file before moving it to the correct file # save it to a tmp file before moving it to the correct file
my $file = $self->{'loadavg_file'}; my $file = $self->{'loadavg_file'};
my $tmpfile = $self->{'loadavg_file'}.$$; my ($dummy_fh, $tmpfile) = ::tempfile(SUFFIX => ".loa", UNLINK => 1);
qx{ ($uptime > $tmpfile && mv $tmpfile $file) & }; qx{ ($uptime > $tmpfile && mv $tmpfile $file) & };
} }
return $self->{'loadavg'}; return $self->{'loadavg'};
@ -3844,10 +3844,10 @@ sub start {
my ($outfh,$errfh,$name); my ($outfh,$errfh,$name);
# To group we create temporary files for STDOUT and STDERR # To group we create temporary files for STDOUT and STDERR
# To avoid the cleanup unlink the files immediately (but keep them open) # To avoid the cleanup unlink the files immediately (but keep them open)
($outfh,$name) = ::tempfile(SUFFIX => ".par"); ($outfh, $name) = ::tempfile(SUFFIX => ".par", UNLINK => 1);
$job->set_stdoutfilename($name); $job->set_stdoutfilename($name);
$opt::files or unlink $name; $opt::files or unlink $name;
($errfh,$name) = ::tempfile(SUFFIX => ".par"); ($errfh, $name) = ::tempfile(SUFFIX => ".par", UNLINK => 1);
unlink $name; unlink $name;
open OUT, '>&', $outfh or ::die_bug("Can't redirect STDOUT: $!"); open OUT, '>&', $outfh or ::die_bug("Can't redirect STDOUT: $!");
@ -3883,13 +3883,12 @@ sub start {
# Bug: # Bug:
# If the command does not read the first char, the temp file # If the command does not read the first char, the temp file
# is not deleted. # is not deleted.
$command = q{ my ($dummy_fh, $tmpfile) = ::tempfile(SUFFIX => ".chr", UNLINK => 1);
eval `echo $SHELL | grep -E "/(t)?csh" > /dev/null && echo setenv _FIRST_CHAR_FILE /tmp/$$.first_char_file || echo export _FIRST_CHAR_FILE=/tmp/$$.first_char_file`; $command = qq{
dd bs=1 count=1 of=$_FIRST_CHAR_FILE >&/dev/null; sh -c 'dd bs=1 count=1 of=$tmpfile 2>/dev/null';
test -s "$_FIRST_CHAR_FILE" || rm "$_FIRST_CHAR_FILE"; test \! -s "$tmpfile" && rm -f "$tmpfile" && exec true;
test -s "$_FIRST_CHAR_FILE" || exit 0; (cat $tmpfile; rm $tmpfile; cat - ) | } .
(cat $_FIRST_CHAR_FILE; rm $_FIRST_CHAR_FILE; cat - ) | } . "($command);";
"($command)";
# The eval is needed to catch exception from open3 # The eval is needed to catch exception from open3
eval { eval {
$pid = ::open3($in, ">&OUT", ">&ERR", $ENV{SHELL}, "-c", $command) || $pid = ::open3($in, ">&OUT", ">&ERR", $ENV{SHELL}, "-c", $command) ||