5 my $confQsub = "/usr/bin/qsub"; # qsub command
6 my $confShell = "/bin/sh"; # the submit script is written for this shell
7 my $confMaxAge = 7*24*60*60; # maximum life time for files in $HOME/.pbsrun: 7 days
8 my $confHistory = "history.txt";
10 my $workdir = gotoWorkDir();
13 getopts('nvN:m:e:q:o:h');
15 printHelpAndExit() if ($opt_h) || (@ARGV < 1);
19 die "pbsrun: -m argument [$opt_m] does not look like an email address\n" unless $opt_m =~ /.*@.*/;
20 $opt_m = "-m abe -M $opt_m";
27 $opt_N = '\"'.$ARGV[0].'\"' unless $opt_N;
29 $opt_q = "-q " . $opt_q if $opt_q;
34 $opt_o = "" if !$opt_o;
35 $opt_q = "" if !$opt_q;
37 my $cmd = join(" ",@ARGV);
40 my $jobName = NewJob();
42 WriteHistory(time(),$jobName,"Command: ",$cmd);
44 open(JOBFILE,">$jobName.script") || die "pbsrun: Could not open $jobName: $!\n";
46 print JOBFILE "#!$confShell\n";
47 print JOBFILE "# This file was generated automatically.\n";
48 print JOBFILE "# on ".(scalar localtime)."\n";
50 print JOBFILE "# restore the user environment\n";
51 foreach my $key (sort keys %ENV)
53 next if ($key eq "DISPLAY");
54 next if ($key eq "HOSTNAME");
55 next if ($key eq "HOSTTYPE");
56 next if ($key eq "LOGNAME");
57 next if ($key eq "MACHTYPE");
58 next if ($key eq "MAIL");
59 next if ($key eq "OSTYPE");
60 next if ($key eq "OLDPWD");
61 next if ($key eq "PWD");
62 next if ($key eq "SHELL");
63 next if ($key eq "SHLVL");
64 next if ($key =~ /^SSH/);
65 next if ($key eq "TERM");
66 next if ($key eq "USER");
67 next if ($key eq "WINDOWID");
68 next if ($key eq "LS_COLORS");
69 next if ($key eq "XAUTHORITY");
70 next if ($key eq "_");
72 print JOBFILE "export $key=\"$val\"\n";
76 print JOBFILE "# restore the user working directory\n";
77 my $pwd = $ENV{"PWD"};
78 print JOBFILE "cd $pwd\n" ;
79 WriteHistory(time(),$jobName,"Pwd: ",$pwd);
81 print JOBFILE "# save the node name\n";
82 print JOBFILE "hostname >$workdir/$jobName.hostname\n";
83 print JOBFILE "# run the user command:\n";
84 print JOBFILE "(".$cmd.")"." >/dev/null 2>/dev/null\n";
85 print JOBFILE "# save the exit status: \n";
86 print JOBFILE "echo \$\? >$workdir/$jobName.status\n";
87 #print JOBFILE "printenv | grep PBS\n";
88 #print JOBFILE "set\n";
89 print JOBFILE "exit \$\?\n";
90 print JOBFILE "# end of file\n";
93 my $outputfile = "$workdir/$jobName.stderr";
95 my $qsubcmd = "$confQsub -S $confShell -r n -j eo -e $outputfile -N $opt_N $opt_o $opt_m $opt_q < $jobName.script";
96 print "pbsrun: $qsubcmd\n" if $opt_v;
98 WriteHistory(time(),$jobName,"Qsub: ",$qsubcmd);
102 WriteHistory(time(),$jobName,"Pbs job: not submitted");
104 print "PBS Job id: not submitted\n";
105 print "Job output: $outputfile\n";
109 my $jobid = submitJob($qsubcmd);
111 die "pbsrun: Failed to submit the pbs job!\n" if !$jobid;
113 WriteHistory(time(),$jobName,"Pbs job: ",$jobid);
115 print "PBS Job id: $jobid\n";
116 print "Job output: $outputfile\n";
123 my @now = localtime();
124 return sprintf("%04d%02d%02d-%02d%02d%02d-%d",
125 1900+$now[5],1+$now[4],$now[3],
126 $now[2],$now[1],$now[0],$$);
132 open(QSUBPROC, "$cmd 2>&1 |") || die "pbsrun: Cannot spawn $confQsub: $!\n";
138 print "qsub: ".$_ if $opt_v;
139 $jobid = $_ if /^\d+/;
143 $jobid =~ s/\n$// if defined $jobid;
150 my $workdir = $ENV{"HOME"}."/.pbsrun";
153 print "pbsrun: no $workdir directory. Making one.\n";
154 mkdir($workdir,0744) || die "pbsrun: Could not make $workdir: $!\n";
156 chdir $workdir || die "pbsrun: Cannot chdir to $workdir: $!\n";
162 my $now = localtime(shift @_);
163 open(TMPOUT,">>$confHistory");
164 print TMPOUT $now,": job ",shift @_,": ",join("",@_),"\n";
171 my @ls = `/bin/ls -1`;
172 foreach $ls (sort @ls)
175 next if $ls eq $confHistory;
178 my $mtime = $stat[9];
180 if ($ls =~ /(.*)\.status/)
182 my $status = `cat $ls`;
184 #print "time $mtime, status [$status]\n";
185 WriteHistory($mtime,$1,"Exit status: ",$status);
190 if ($ls =~ /(.*)\.hostname/)
192 my $hostname = `cat $ls`;
194 #print "time $mtime, hostname [$hostname]\n";
195 WriteHistory($mtime,$1,"Started on: ",$hostname);
200 my $age = $now - $mtime;
202 #print "file [$ls] age: [$age]\n";
204 if ($age > $confMaxAge)
213 print "Usage: pbsrun [-q queue] [-N job_name] command...\n";
214 print " Options: \n";
215 print " [-h] Prints out this message.\n";
216 print " [-N job_name] name this job.\n";
217 print " [-m user\@host] emails you when the job begin, ends or aborts.\n";
218 print " [-o qsub_options] passes options to the call to qsub.\n";
219 print " [-q queue] selects a queue.\n";
220 print " [-n] test mode: do not run any pbs commands.\n";
221 print " [-k] keep, do not delete temp files.\n";
222 print " [-v] be verbose, show progress.\n";