1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
|
#!/bin/sh
#------------------------------------------------------------------------------
# ========= |
# \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
# \\ / O peration |
# \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
# \\/ M anipulation |
#-------------------------------------------------------------------------------
# License
# This file is part of OpenFOAM.
#
# OpenFOAM is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
#
# Script
# foamCheckJobs
#
# Description
# Uses runningJobs/, finishedJobs/ and foamProcessInfo to create stateFile.
# stateFile contains per pid information on state of process. Format:
# pid state command
#
# where state is one of 'RUNN', 'SUSP', 'OTHR', 'FINI', 'ABRT' ('PEND')
# (first three are from foamProcessInfo, others from jobInfo files)
# (PEND is special state from when user has submitted but no jobInfo
# file yet. Not supported by this script yet)
#
#------------------------------------------------------------------------------
Script=${0##*/}
#-------------------------------------------------------------------------------
#- User settings
#- Number of days for files to be considered old
NDAYSLIMIT=7
#-------------------------------------------------------------------------------
#- work file
TMPFILE=/tmp/${Script}$$.tmp
#- work dir. Needs to be accessible for all machines
MACHDIR=$HOME/.OpenFOAM/${Script}
DEFSTATEFILE=$HOME/.OpenFOAM/foamCheckJobs.out
if [ `uname -s` = Linux ]
then
ECHO='echo -e'
else
ECHO='echo'
fi
usage() {
cat<<USAGE
Usage: $Script [stateFile]
This program checks all the locks in the FOAM_JOB_DIR directory to see if
their processes are still running. Processes will not release their
lock if they exit abnormally. This program will try to obtain process
information on the machine the process ran on and release the lock
if the program is no longer running.
Note: all machines have to be reachable using ssh.
The output from checking all running jobs is collected in an optional
file.
FILES:
\$FOAM_JOB_DIR/runningJobs locks for running processes
/finishedJobs locks for finished processes
USAGE
exit 1
}
#-------------------------------------------------------------------------------
#
# Functions
#
#-------------------------------------------------------------------------------
# getRawEntry dictionary entry
# Prints value of dictionary entry
getRawEntry() {
grep -v '^//' $1 | grep "^[ \t]*$2 " | sed -e "s/^[ \t]*$2 [ ]*//"
}
# getEntry dictionary entry
# Like getRawEntry but strips " and ending ';'
getEntry() {
getRawEntry $1 $2 | sed -e 's/^"//' -e 's/;$//' -e 's/"$//'
}
# notEmpty directory
# Returns 0 if directory contains files/directories
notEmpty() {
if [ "`ls $1`" ]
then
return 0
else
return 1
fi
}
# dayDiff <date string 1> <date string 2>
# Prints number of days between the two
# Eg. dayDiff "Jan 10 2002" "Dec 28 1999"
# ==> 13
dayDiff() {
date -d "$1" > /dev/null 2>&1
if [ $? -ne 0 ]
then
#- option '-d' on date not supported. Give up.
echo "0"
else
year1=`echo "$1" | awk '{print $3}'`
year2=`echo "$2" | awk '{print $3}'`
day1=`date -d "$1" "+%j"`
day2=`date -d "$2" "+%j"`
nYears=`expr $year1 - $year2`
tmp1=`expr $nYears \* 365`
tmp2=`expr $day1 - $day2`
expr $tmp1 + $tmp2
fi
}
#dayDiff "`date '+%b %d %Y'`" "Dec 28 2001"
# getAllJobs jobInfoDirectory
# Prints list of all jobs in directory (e.g. runningJobs/)
# Also handles 'slaves' entries in jobInfo:
# slaves 1 ( penfold.23766 );
getAllJobs() {
if notEmpty $1
then
jobs=$1/*
for f in $jobs
do
line=`grep '^[ ]*slaves' $f 2>/dev/null`
if [ $? -eq 0 ]
then
slaveJobs=`echo "$line" | sed -e 's/.*(\(.*\)).*/\1/'`
jobs="$jobs $slaveJobs"
fi
done
else
jobs=''
fi
echo "$jobs"
}
# releaseLock jobId lockFile
# Releases lock on jobId
releaseLock () {
if [ -f $2 ]
then
#- move lock to finishedJobs
mv $2 $FOAM_JOB_DIR/finishedJobs/
fi
echo "Lock on job $1 released."
}
#-------------------------------------------------------------------------------
#
# Main
#
#-------------------------------------------------------------------------------
if [ $# -eq 1 ]
then
if [ "$1" = "-h" -o "$1" = "-help" ]
then
usage
fi
STATEFILE="$1"
elif [ $# -eq 0 ]
then
STATEFILE=${STATEFILE:-$DEFSTATEFILE}
else
usage
fi
#- Check a few things
if [ ! "$FOAM_JOB_DIR" ]
then
echo "$Script : FOAM_JOB_DIR environment variable not set."
echo
exit 1
fi
if [ ! -d "$FOAM_JOB_DIR" ]
then
echo "$Script : directory does not exist."
echo " FOAM_JOB_DIR=$FOAM_JOB_DIR"
echo
exit 1
fi
if [ ! -d "$FOAM_JOB_DIR/runningJobs" -o ! -d "$FOAM_JOB_DIR/finishedJobs" ]
then
echo "$Script : invalid directory."
echo " FOAM_JOB_DIR=$FOAM_JOB_DIR"
echo
exit 1
fi
#- obtain rsh method
RSH='ssh'
echo "Using remote shell type : $RSH"
echo ""
echo "Collecting information on jobs in"
echo " $FOAM_JOB_DIR"
echo ""
#- Collect machine names into $TMPFILE
# Also handles 'slaves' entry in jobInfo:
rm -f $TMPFILE; touch $TMPFILE
RUNJOBS=`getAllJobs $FOAM_JOB_DIR/runningJobs`
for f in $RUNJOBS
do
machinePid=`basename $f | sed -e 's/^"//' -e 's/"$//'`
machine=`echo "$machinePid" | sed -e 's/\.[0-9][0-9]*$//'`
pid=`echo "$machinePid" | sed -e 's/.*\.\([0-9][0-9]*\)$/\1/'`
fgrep "$machine" $TMPFILE >/dev/null 2>&1
if [ $? -ne 0 ]
then
echo "$machine" >> $TMPFILE
fi
done
echo "Found machines:"
cat $TMPFILE
echo ""
#- Collect process info on all machines, one file per machine
mkdir -p $MACHDIR
cnt=1
while true
do
machine=`sed -n -e "${cnt}p" $TMPFILE`
if [ ! "$machine" ]
then
break
fi
machFile=$MACHDIR/$machine
rm -f $machFile
echo "Contacting $machine to collect process information:"
if [ $machine = `hostname` ]
then
echo " foamProcessInfo $machFile"
foamProcessInfo $machFile >/dev/null 2>&1
else
echo " $RSH $machine foamProcessInfo $machFile"
$RSH $machine foamProcessInfo $machFile >/dev/null 2>&1
fi
if [ $? -ne 0 -o ! -s $machFile ]
then
echo "** Failed collecting process information on $machine."
echo "Check $machFile and run foamProcessInfo by hand"
rm -f $machFile
else
echo "Succesfully collected information in $machFile ..."
fi
cnt=`expr $cnt + 1`
done
echo ""
#- Construct state for runningJobs; move non runnning jobs to finishedJobs
releaseAll=''
rm -f $STATEFILE
for f in $RUNJOBS
do
machinePid=`basename $f`
machine=`echo $machinePid | sed -e 's/\.[0-9][0-9]*$//'`
pid=`echo $machinePid | sed -e 's/.*\.\([0-9][0-9]*\)$/\1/'`
machFile=$MACHDIR/$machine
if [ -r $machFile ]
then
entry=`grep "^$pid " $machFile 2>/dev/null`
if [ $? -ne 0 -o ! "$entry" ]
then
if [ "$releaseAll" ]
then
releaseLock $machinePid $f
else
echo "Job $machinePid seems to be no longer running. Release lock? (y/a)\c"
read answ
if [ "${answ:-y}" = 'y' ]
then
releaseLock $machinePid $f
elif [ "${answ:-y}" = 'a' ]
then
releaseAll='yes'
releaseLock $machinePid $f
else
state='OTHR'
echo "$machinePid $state" >> $STATEFILE
fi
fi
else
state=`echo "$entry" | awk '{print $2}'`
echo "$machinePid $state" >> $STATEFILE
fi
fi
done
#- Collect old jobs in finishedJobs
OLDFILES=`find $FOAM_JOB_DIR/finishedJobs -mtime +$NDAYSLIMIT -print`
#- Construct state for finishedJobs and check on date of files.
if notEmpty $FOAM_JOB_DIR/finishedJobs
then
dateNow=`date '+%b %d %Y'`
for f in $FOAM_JOB_DIR/finishedJobs/*
do
sz=`ls -s $f | awk '{print $1}'`
if [ "$sz" -gt 0 ]
then
machinePid=`basename $f`
machine=`echo $machinePid | sed -e 's/\.[0-9][0-9]*$//'`
pid=`echo $machinePid | sed -e 's/.*\.\([0-9][0-9]*\)$/\1/'`
end=`getEntry $f endDate`
if [ ! "$end" ]
then
state='ABRT'
else
nDaysOld=`dayDiff "$dateNow" "$end"`
if [ "$nDaysOld" -gt $NDAYSLIMIT ]
then
OLDFILES="$OLDFILES $f"
fi
state='FINI'
fi
echo "$machinePid $state" >> $STATEFILE
fi
done
fi
#- Remove old locks
nOldFiles=`echo "$OLDFILES" | wc -w`
if [ "$nOldFiles" -gt 0 ]
then
echo "You seem to have $nOldFiles locks older than $NDAYSLIMIT days in finishedJobs/"
$ECHO "Do you want to remove these? (y)\c"
read answ
if [ "${answ:-y}" = 'y' ]
then
rm -f $OLDFILES
fi
fi
rm -f $TMPFILE
rm -r $MACHDIR
echo ""
echo "Updated stateFile:"
echo " $STATEFILE"
echo ""
#------------------------------------------------------------------------------
|