#!/usr/bin/perl # # FILE: delete.perl ArDean Leith June. 2001 # # PURPOSE: Put final data for job in publisher log # # LOCATION: /net/bali/usr1/spider/pubsub # # USAGE: delete.perl 1234 /usr1/clyde/Refinement/pubsub.log # # INPUT: # job id (argument #1) # log file (argument #2) # # OUTPUT: # job id (returned) # get the jobid from arguments $deljobnum = @ARGV[0]; # get the log file name from arguments $log = @ARGV[1]; #print "log: $log \n"; # Terminate this jobid in the publisher log #print "Finishing job: $deljobnum \n"; # Open LOG file open(LOG, "+< $log") || die "Delete.perl can not open: $log \n";; #lock the opened log file flock(LOG,2); #Read the log and rewrite the log for terminating job $out = ''; while () { $line = $_; #print "line: $line \n"; chop($line); ($jobnumber,$remaining) = split(/\b\s+/,$line,1); $gotruntime = ($line =~ m/.*Runtime.*/ ); # Do not alter this line if already done if ( ($jobnumber == $deljobnum) && (!$gotruntime) ) { # Found this job, list ending time in log ($jobnumber,$machine,$quedtime,$starttime,$remaining) = split(/\b\s+/,$line,5); # Get current time $endtime = time(); #($sec,$min,$hour,$mday,$month,$year,$more) = localtime($endtime); #$time = sprintf("%04d-%02d-%02d %02d:%02d:%02d", # $year+1900,$month+1,$mday, $hour,$min,$sec); # Find elapsed run time $runtime = $endtime - $starttime; #$out .= "$deljobnum $machine $quedtime $starttime $remaining (Ended: $time) (Runtime: $runtime) \n"; $out .= "$deljobnum $machine $quedtime $starttime $remaining (Runtime: $runtime) \n"; while () # read remining lines from QUE file { # Keep line in LOG file $out .= $_; } last; # Finished reading now } else { # different job or not a locked job $out .= $_; } } seek(LOG,0,0); # Goto log beginning again print LOG $out; # Put all output lines in log file truncate(LOG, tell(LOG)); # Unlock the log file flock(LOG,8); close(LOG); print "Finished job: $deljobnum \n"; # ---------------------- RELEASE (NOT USED YET!!!) -------------------------- # NOT ACTIVATED BELOW (put in delte above to rea-activate) $ret = 0; if ($delgroup) { # See if we deleted last of this group $foundgroup = &release($delgroup,$log,$command); #print " Foundgroup: $foundgroup Command: $command \n"; if ((!$foundgroup) && $command) { # Execute command to be run if deleted last of this group #print "echo Executing command: $command \n" ; #system("echo Executing command: $command \n"); system($command); } } exit $ret; sub release { if (@_ > 3) {die "Usage: $0 jobgroup logfile command Where. \n jobgroup is: log jobgroup to be watched.\n logfile is: publisher log file.\n command is: command to be executed. \n" } $check = 5; $jobgroup = $_[0]; $logfile = $_[1]; $command = $_[2]; #print "Group: $jobgroup Command: $command \n"; open(LOG , "< $logfile") || die "Can not open: $logfile \n"; $foundgroup = 0; #Exclusive lock=2, Non-blocking request=4, Free lock=8 unless (flock(LOG, 2)) #block the input/output file { die " Can not lock: $logfile \n";} while () #read input { ($jobid,$grp,$rest) = split(/\b\s+/,$_,3); #($jobid,$grp,$rest) =~ /(-?\d+)(-?\d+) (.*)/; # split at spaces #$n++; #print "input[".$n."]: " . $_."\n"; #print "grp[".$n."]: " . $grp . "\n"; #print "jobid[".$n."]: " . $jobid . "\n"; if ($grp == $jobgroup) { # found this jobgroup, exit now $foundgroup = 1; last; } } flock(LOG,8); #unlock log file close(LOG); #print "foundgroup: $foundgroup grp: $grp\n"; if (! $foundgroup) # jobgroup not found this time { last; } return $foundgroup; }