#!/usr/bin/perl -w use strict; my $sudo = ''; $sudo = "sudo " if ($> != 0); my $histf = "$ENV{HOME}/bpdbjobs_history"; my $tempf = "$histf.$$"; my $backf = "$histf.bak"; unlink $backf if (-f $backf); link $histf, $backf; open(HISTF, "<$histf") or die "couldn't open $histf for reading"; my $lastrec = 0; my ($jobid, $state, %active); while () { ($jobid, $state) = (split(',', $_))[0,2]; $lastrec = $jobid if ($jobid > $lastrec); $active{$jobid} = $_ if ($state < 3); } close HISTF; open(TEMPF, ">$tempf") or die "couldn't open $tempf for writing"; my %newer; foreach (`$sudo bpdbjobs -all_columns`) { $jobid = (split(',', $_))[0]; if ($jobid > $lastrec) { print TEMPF $_; } elsif (defined($active{$jobid})) { $newer{$jobid} = 1; print TEMPF $_; } } open(HISTF, "<$histf") or die "couldn't open $histf for reading"; while () { $jobid = (split(',', $_))[0]; print TEMPF $_ unless defined($newer{$jobid}); } close HISTF; close TEMPF; unlink $histf or die "couldn't remove old $histf"; link $tempf, $histf or die "couldn't move $tempf to $histf"; unlink $tempf or die "couldn't remove $tempf"; # save on space; it's safe at this point, we're sure the process # works after using for a month or two, and we're sure by this point # in the code that we're not losing data doing this unlink $backf;