]> git.pld-linux.org Git - packages/OpenPBS.git/blob - pbsrun
- dropped pre-cvs changelog
[packages/OpenPBS.git] / pbsrun
1 #!/usr/bin/perl -w
2
3 use Getopt::Std;
4
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";
9
10 my $workdir = gotoWorkDir();
11 cleanup();
12
13 getopts('nvN:m:e:q:o:h');
14
15 printHelpAndExit() if ($opt_h) || (@ARGV < 1);
16
17 if ($opt_m)
18   {
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";
21   }
22 else
23   {
24     $opt_m = "-m n";
25   }
26
27 $opt_N = '\"'.$ARGV[0].'\"' unless $opt_N;
28
29 $opt_q = "-q " . $opt_q        if $opt_q;
30
31 0 if $opt_n;
32 0 if $opt_h;
33 0 if $opt_v;
34 $opt_o = "" if !$opt_o;
35 $opt_q = "" if !$opt_q;
36
37 my $cmd = join(" ",@ARGV);
38
39
40 my $jobName = NewJob();
41
42 WriteHistory(time(),$jobName,"Command: ",$cmd);
43
44 open(JOBFILE,">$jobName.script") || die "pbsrun: Could not open $jobName: $!\n";
45
46 print JOBFILE "#!$confShell\n";
47 print JOBFILE "# This file was generated automatically.\n"; 
48 print JOBFILE "# on ".(scalar localtime)."\n";
49 print JOBFILE "#\n";
50 print JOBFILE "# restore the user environment\n";
51 foreach my $key (sort keys %ENV)
52   {
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 "_");
71     my $val = $ENV{$key};
72     print JOBFILE "export $key=\"$val\"\n";
73   }
74
75 print JOBFILE "#\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);
80
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";
91 close JOBFILE;
92
93 my $outputfile = "$workdir/$jobName.stderr";
94
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;
97
98 WriteHistory(time(),$jobName,"Qsub: ",$qsubcmd);
99
100 if ($opt_n)
101   {
102     WriteHistory(time(),$jobName,"Pbs job: not submitted");
103
104     print "PBS Job id: not submitted\n";
105     print "Job output: $outputfile\n";
106   }
107 else
108   {
109     my $jobid = submitJob($qsubcmd);
110     
111     die "pbsrun: Failed to submit the pbs job!\n" if !$jobid;
112     
113     WriteHistory(time(),$jobName,"Pbs job: ",$jobid);
114     
115     print "PBS Job id: $jobid\n";
116     print "Job output: $outputfile\n";
117   }
118
119 exit 0;
120
121 sub NewJob
122   {
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],$$);
127   }
128
129 sub submitJob
130   {
131     my $cmd = shift @_;
132     open(QSUBPROC, "$cmd 2>&1 |") || die "pbsrun: Cannot spawn $confQsub: $!\n";
133
134     my $jobid;
135     
136     while (<QSUBPROC>)
137       {
138         print "qsub: ".$_ if $opt_v;
139         $jobid = $_ if /^\d+/;
140       }
141     close QSUBPROC;
142
143     $jobid =~ s/\n$// if defined $jobid;
144
145     return $jobid;
146   }
147
148 sub gotoWorkDir
149   {
150     my $workdir = $ENV{"HOME"}."/.pbsrun";
151     if (! -d $workdir)
152       {
153         print "pbsrun: no $workdir directory. Making one.\n";
154         mkdir($workdir,0744) || die "pbsrun: Could not make $workdir: $!\n";
155       }
156     chdir $workdir || die "pbsrun: Cannot chdir to $workdir: $!\n";
157     return $workdir;
158   }
159
160 sub WriteHistory
161   {
162     my $now = localtime(shift @_);
163     open(TMPOUT,">>$confHistory");
164     print TMPOUT $now,": job ",shift @_,": ",join("",@_),"\n";
165     close TMPOUT;
166   }
167     
168 sub cleanup
169   {
170     my $now = time();
171     my @ls = `/bin/ls -1`;
172     foreach $ls (sort @ls)
173       {
174         chop $ls;
175         next if $ls eq $confHistory;
176
177         my @stat = stat $ls;
178         my $mtime = $stat[9];
179
180         if ($ls =~ /(.*)\.status/)
181           {
182             my $status = `cat $ls`;
183             chop $status;
184             #print "time $mtime, status [$status]\n";
185             WriteHistory($mtime,$1,"Exit status: ",$status);
186             unlink $ls;
187             next;
188           }
189
190         if ($ls =~ /(.*)\.hostname/)
191           {
192             my $hostname = `cat $ls`;
193             chop $hostname;
194             #print "time $mtime, hostname [$hostname]\n";
195             WriteHistory($mtime,$1,"Started on: ",$hostname);
196             unlink $ls;
197             next;
198           }
199
200         my $age = $now - $mtime;
201
202         #print "file [$ls] age: [$age]\n";
203
204         if ($age > $confMaxAge)
205           {
206             unlink $ls;
207           }
208       }
209   }
210
211 sub printHelpAndExit
212 {
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";
223     exit 1;
224 }
225
226 #end file
This page took 0.089322 seconds and 3 git commands to generate.