]> git.pld-linux.org Git - packages/OpenPBS.git/blame - pbsrun
- dropped pre-cvs changelog
[packages/OpenPBS.git] / pbsrun
CommitLineData
b320f022 1#!/usr/bin/perl -w
2
3use Getopt::Std;
4
5my $confQsub = "/usr/bin/qsub"; # qsub command
6my $confShell = "/bin/sh"; # the submit script is written for this shell
7my $confMaxAge = 7*24*60*60; # maximum life time for files in $HOME/.pbsrun: 7 days
8my $confHistory = "history.txt";
9
10my $workdir = gotoWorkDir();
11cleanup();
12
13getopts('nvN:m:e:q:o:h');
14
15printHelpAndExit() if ($opt_h) || (@ARGV < 1);
16
17if ($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 }
22else
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
310 if $opt_n;
320 if $opt_h;
330 if $opt_v;
34$opt_o = "" if !$opt_o;
35$opt_q = "" if !$opt_q;
36
37my $cmd = join(" ",@ARGV);
38
39
40my $jobName = NewJob();
41
42WriteHistory(time(),$jobName,"Command: ",$cmd);
43
44open(JOBFILE,">$jobName.script") || die "pbsrun: Could not open $jobName: $!\n";
45
46print JOBFILE "#!$confShell\n";
47print JOBFILE "# This file was generated automatically.\n";
48print JOBFILE "# on ".(scalar localtime)."\n";
49print JOBFILE "#\n";
50print JOBFILE "# restore the user environment\n";
51foreach 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
75print JOBFILE "#\n";
76print JOBFILE "# restore the user working directory\n";
77my $pwd = $ENV{"PWD"};
78print JOBFILE "cd $pwd\n" ;
79WriteHistory(time(),$jobName,"Pwd: ",$pwd);
80
81print JOBFILE "# save the node name\n";
82print JOBFILE "hostname >$workdir/$jobName.hostname\n";
83print JOBFILE "# run the user command:\n";
84print JOBFILE "(".$cmd.")"." >/dev/null 2>/dev/null\n";
85print JOBFILE "# save the exit status: \n";
86print JOBFILE "echo \$\? >$workdir/$jobName.status\n";
87#print JOBFILE "printenv | grep PBS\n";
88#print JOBFILE "set\n";
89print JOBFILE "exit \$\?\n";
90print JOBFILE "# end of file\n";
91close JOBFILE;
92
93my $outputfile = "$workdir/$jobName.stderr";
94
95my $qsubcmd = "$confQsub -S $confShell -r n -j eo -e $outputfile -N $opt_N $opt_o $opt_m $opt_q < $jobName.script";
96print "pbsrun: $qsubcmd\n" if $opt_v;
97
98WriteHistory(time(),$jobName,"Qsub: ",$qsubcmd);
99
100if ($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 }
107else
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
119exit 0;
120
121sub 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
129sub 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
148sub 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
160sub WriteHistory
161 {
162 my $now = localtime(shift @_);
163 open(TMPOUT,">>$confHistory");
164 print TMPOUT $now,": job ",shift @_,": ",join("",@_),"\n";
165 close TMPOUT;
166 }
167
168sub 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
211sub 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.077389 seconds and 4 git commands to generate.