#!/opt/sice/1.0/bin/perl # # Copyright (c) 2004,2005 Scalable Informatics LLC # # This is not free or open source software. Please see license # for more details. # #$ -S /usr/bin/perl ### options use strict; ### constants use constant true => (1==1); use constant false=> (1==0); use constant config_file_default => '/opt/sice/1.0/etc/dynaq.ini'; use constant sleep_time => 10; ### perl modules use Config::IniFiles; use Data::Dumper; use Getopt::Long; use English; use Sys::Hostname; use IPC::Run qw( start pump run finish timeout ) ; ### variables my ($debug,$verbose,$command_line,$conf,$qstat,$qstat_options); my ($job_id,$username,$arch,$shell,$cr,$wait,$sleep_interval); my ($file_handle,$rc,$config,,$run_handle,@cmd,$in,$out,$err); my ($qacct,$state,$current_state); ## start processing # default values $debug = false; $verbose = false; $command_line = ""; $sleep_interval = sleep_time; $conf = config_file_default; # parse command line Getopt::Long::Configure("pass_through"); $rc = GetOptions ( "debug" => \$debug, "verbose" => \$verbose, "conf=s" => \$conf, "job=i" => \$job_id, "username=s" => \$username, "wait=i" => \$wait, "sleep=i" => \$sleep_interval ); $verbose = true if ($debug); # make sure config file exists, and is readable if (!(-e $conf)) { die "FATAL ERROR: File=\'".$conf."\' must exist prior to the start of the run\n"; } if (!(-r $conf)) { die "FATAL ERROR: File=\'".$conf."\' is not readable by user \'".$EUID."\'\n"; } $config = new Config::IniFiles( -file => $conf ); $arch = &arch; printf STDERR "D[%s]: Architecture : %s\n",$$,$arch if ($debug); $qstat = $config->val($arch,'qstat'); $qacct = $config->val($arch,'qacct');; printf STDERR "D[%s]: qstat : %s\n",$$,$qstat if ($debug); $shell = $config->val($arch,'shell'); $cr="\n"; printf STDERR "D[%s]: job_id : \'%s\'\n",$$,$job_id if ($debug); printf STDERR "D[%s]: wait for job_id: \'%s\'\n",$$,$wait if (($debug) && ($wait)); printf STDERR "D[%s]: sleep interval : \'%s\'\n",$$,$sleep_interval if ($debug); $state = &test_job($job_id); #printf "Dumper: %s\n",Dumper($state); printf "%s\n",$state->{_state} if ($verbose); exit if ($state->{_state} =~ /completed/i); exit if ($state->{_state} =~ /not found/i); do { $current_state = $state->{_state}; sleep($sleep_interval); $state = &test_job($job_id); printf "D[%s]: previous = \'%s\' ,state = \'%s\'\n",$$,$current_state,$state->{_state} if ($debug); printf "D[%s]: state : \'%s\'\n",$$,$state->{_state} if (($verbose) && (!$debug)); } until ( ( $state->{_state} =~ /completed/i ) || ( ($state->{_state} =~ /unknown/i) && ($current_state =~ /running/i ) ) ); exit; sub arch { my ($rc,$arch,$type); chomp($rc=`uname -s`); if ($rc =~ /^Linux$/) { $rc.="-"; chomp($type=`uname -m`); } else { $type=$rc; $rc=""; } $arch=$rc.$type; return $arch; } sub test_job { my $job_id = shift; my $previous = shift; my (@cmd,$run_handle,$in,$out,$err,$ret); push @cmd, $qstat; if ($job_id) { push @cmd, '-j'; push @cmd, $job_id;} if ($debug) { foreach (@cmd) { printf STDERR "D[%s]: command = \'%s\'\n",$$,$_; } } $run_handle = run \@cmd, \$in, \$out, \$err, timeout(sleep_time); #printf STDERR "D[%s]: out = %s\n",$$,$out if ($debug); #printf STDERR "D[%s]: err = %s\n",$$,$err if ($debug); #check for completion. if ($out =~ /usage/) { $ret->{err}=$err; $ret->{out}=$out; $ret->{_state}="running"; return $ret; } $ret->{err}=$err; $ret->{out}=$out; $ret->{_state}="unknown"; return $ret; }