2 #---------------------------------*- sh -*-------------------------------------
4 # \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
6 # \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
8 #------------------------------------------------------------------------------
10 # This file is part of OpenFOAM.
12 # OpenFOAM is free software: you can redistribute it and/or modify it
13 # under the terms of the GNU General Public License as published by
14 # the Free Software Foundation, either version 3 of the License, or
15 # (at your option) any later version.
17 # OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
18 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 # You should have received a copy of the GNU General Public License
23 # along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
29 # Uses runningJobs/, finishedJobs/ and foamProcessInfo to create stateFile.
30 # stateFile contains per pid information on state of process. Format:
33 # where state is one of 'RUNN', 'SUSP', 'OTHR', 'FINI', 'ABRT' ('PEND')
34 # (first three are from foamProcessInfo, others from jobInfo files)
35 # (PEND is special state from when user has submitted but no jobInfo
36 # file yet. Not supported by this script yet)
38 #------------------------------------------------------------------------------
42 #-------------------------------------------------------------------------------
45 #- Number of days for files to be considered old
47 #-------------------------------------------------------------------------------
50 TMPFILE
=/tmp
/${PROGNAME}$$.tmp
51 #- work dir. Needs to be accessible for all machines
52 MACHDIR
=$HOME/.OpenFOAM
/${PROGNAME}
53 DEFSTATEFILE
=$HOME/.OpenFOAM
/foamCheckJobs.out
56 if [ `uname -s` = Linux
]
64 #-------------------------------------------------------------------------------
68 #-------------------------------------------------------------------------------
70 # getRawEntry dictionary entry
71 # Prints value of dictionary entry
73 grep -v '^//' $1 |
grep "^[ \t]*$2 " |
sed -e "s/^[ \t]*$2 [ ]*//"
76 # getEntry dictionary entry
77 # Like getRawEntry but strips " and ending ';'
79 getRawEntry
$1 $2 |
sed -e 's/^"//' -e 's/;$//' -e 's/"$//'
83 # Returns 0 if directory contains files/directories
85 if [ "`ls $1`" ]; then
92 # dayDiff <date string 1> <date string 2>
93 # Prints number of days between the two
94 # Eg. dayDiff "Jan 10 2002" "Dec 28 1999"
97 date -d "$1" > /dev
/null
2>&1
99 #- option '-d' on date not supported. Give up.
102 year1
=`echo "$1" | awk '{print $3}'`
103 year2
=`echo "$2" | awk '{print $3}'`
104 day1
=`date -d "$1" "+%j"`
105 day2
=`date -d "$2" "+%j"`
107 nYears
=`expr $year1 - $year2`
108 tmp1
=`expr $nYears \* 365`
109 tmp2
=`expr $day1 - $day2`
113 #dayDiff "`date '+%b %d %Y'`" "Dec 28 2001"
116 # getAllJobs jobInfoDirectory
117 # Prints list of all jobs in directory (e.g. runningJobs/)
118 # Also handles 'slaves' entries in jobInfo:
119 # slaves 1 ( penfold.23766 );
125 line
=`grep '^[ ]*slaves' $f 2>/dev/null`
126 if [ $?
-eq 0 ]; then
127 slaveJobs
=`echo "$line" | sed -e 's/.*(\(.*\)).*/\1/'`
128 jobs="$jobs $slaveJobs"
137 # releaseLock jobId lockFile
138 # Releases lock on jobId
141 #- move lock to finishedJobs
142 mv $2 $FOAM_JOB_DIR/finishedJobs
/
144 $ECHO "Lock on job $1 released."
150 Usage: $PROGNAME [stateFile]
152 This program checks all the locks in the license directory to see if
153 their processes are still running. Processes will not release their
154 lock if they exit abnormally. This program will try to obtain process
155 information on the machine the process ran on and release the lock
156 if the program is no longer running.
158 Requirements: the environment variable FOAM_JOB_DIR needs to point to the
159 license directory and all machines have to be reachable using ssh.
161 The output from checking all running jobs is collected in an optional
165 \$FOAM_JOB_DIR/runningJobs locks for running processes
166 /finishedJobs ,, finished processes
171 #-------------------------------------------------------------------------------
175 #-------------------------------------------------------------------------------
177 #- Check a few things
179 if [ ! "$FOAM_JOB_DIR" ]; then
180 $ECHO "$PROGNAME : FOAM_JOB_DIR environment variable not set."
181 $ECHO "This should point to your central license directory."
185 if [ ! -d "$FOAM_JOB_DIR" ]; then
186 $ECHO "$PROGNAME : The license directory accoring to FOAM_JOB_DIR is not valid."
187 $ECHO "FOAM_JOB_DIR: $FOAM_JOB_DIR"
190 if [ ! -d "$FOAM_JOB_DIR/runningJobs" -o ! -d "$FOAM_JOB_DIR/finishedJobs" ]; then
191 $ECHO "$PROGNAME : The license directory according to FOAM_JOB_DIR is not valid."
192 $ECHO "FOAM_JOB_DIR: $FOAM_JOB_DIR"
197 if [ $# -eq 1 ]; then
199 elif [ $# -eq 0 ]; then
200 STATEFILE
=${STATEFILE:-$DEFSTATEFILE}
208 echo "Using remote shell type : $RSH"
211 echo "Collecting information on jobs in"
212 echo " $FOAM_JOB_DIR"
216 #- Collect machine names into $TMPFILE
217 # Also handles 'slaves' entry in jobInfo:
219 rm -f $TMPFILE; touch $TMPFILE
220 RUNJOBS
=`getAllJobs $FOAM_JOB_DIR/runningJobs`
223 machinePid
=`basename $f`
224 machine
=`echo $machinePid | sed -e 's/\.[0-9][0-9]*$//'`
225 pid
=`echo $machinePid | sed -e 's/.*\.\([0-9][0-9]*\)$/\1/'`
227 fgrep
"$machine" $TMPFILE >/dev
/null
2>&1
228 if [ $?
-ne 0 ]; then
229 $ECHO "$machine" >> $TMPFILE
232 $ECHO "Found machines:"
238 #- Collect process info on all machines, one file per machine
244 machine
=`sed -n -e "${cnt}p" $TMPFILE`
245 if [ ! "$machine" ]; then
249 machFile
=$MACHDIR/$machine
251 $ECHO "Contacting $machine to collect process information:"
252 if [ $machine = `hostname` ]; then
253 $ECHO " foamProcessInfo $machFile"
254 foamProcessInfo
$machFile >/dev
/null
2>&1
256 $ECHO " $RSH $machine foamProcessInfo $machFile"
257 $RSH $machine foamProcessInfo
$machFile >/dev
/null
2>&1
259 if [ $?
-ne 0 -o ! -s $machFile ]; then
260 $ECHO "** Failed collecting process information on $machine."
261 $ECHO "Check $machFile and run foamProcessInfo by hand"
264 $ECHO "Succesfully collected information in $machFile ..."
272 #- Construct state for runningJobs; move non runnning jobs to finishedJobs
278 machinePid
=`basename $f`
279 machine
=`echo $machinePid | sed -e 's/\.[0-9][0-9]*$//'`
280 pid
=`echo $machinePid | sed -e 's/.*\.\([0-9][0-9]*\)$/\1/'`
282 machFile
=$MACHDIR/$machine
283 if [ -r $machFile ]; then
284 entry
=`grep "^$pid " $machFile 2>/dev/null`
285 if [ $?
-ne 0 -o ! "$entry" ]; then
286 if [ "$releaseAll" ]; then
287 releaseLock
$machinePid $f
289 $ECHO "Job $machinePid seems to be no longer running. Release lock? (y/a)\c"
291 if [ "${answ:-y}" = 'y' ]; then
292 releaseLock
$machinePid $f
293 elif [ "${answ:-y}" = 'a' ]; then
295 releaseLock
$machinePid $f
298 $ECHO "$machinePid $state" >> $STATEFILE
302 state
=`echo "$entry" | awk '{print $2}'`
303 $ECHO "$machinePid $state" >> $STATEFILE
310 #- Collect old jobs in finishedJobs
312 OLDFILES
=`find $FOAM_JOB_DIR/finishedJobs -mtime +$NDAYSLIMIT -print`
314 #- Construct state for finishedJobs and check on date of files.
316 if notEmpty
$FOAM_JOB_DIR/finishedJobs
; then
317 dateNow
=`date '+%b %d %Y'`
318 for f
in $FOAM_JOB_DIR/finishedJobs
/*
320 sz
=`ls -s $f | awk '{print $1}'`
321 if [ "$sz" -gt 0 ]; then
322 machinePid
=`basename $f`
323 machine
=`echo $machinePid | sed -e 's/\.[0-9][0-9]*$//'`
324 pid
=`echo $machinePid | sed -e 's/.*\.\([0-9][0-9]*\)$/\1/'`
326 end
=`getEntry $f endDate`
327 if [ ! "$end" ]; then
330 nDaysOld
=`dayDiff "$dateNow" "$end"`
331 if [ "$nDaysOld" -gt $NDAYSLIMIT ]; then
332 OLDFILES
="$OLDFILES $f"
338 $ECHO "$machinePid $state" >> $STATEFILE
346 nOldFiles
=`echo "$OLDFILES" | wc -w`
347 if [ "$nOldFiles" -gt 0 ]; then
348 $ECHO "You seem to have $nOldFiles locks older than $NDAYSLIMIT days in finishedJobs/"
349 $ECHO "Do you want to remove these? (y)\c"
351 if [ "${answ:-y}" = 'y' ]; then
361 $ECHO "Updated stateFile:"
365 #------------------------------------------------------------------------------