2 #------------------------------------------------------------------------------
4 # \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
6 # \\ / A nd | Copyright held by original author
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 the
14 # Free Software Foundation; either version 2 of the License, or (at your
15 # 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, write to the Free Software Foundation,
24 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 # Ends running job on current machine. Called with root,case,pid.
31 # - checks if pid exists
32 # - modifies controlDict
35 # - controlDict modified
36 # to restore controlDict
38 #------------------------------------------------------------------------------
40 PROGNAME
=`basename $0`
43 #------------------------------------------------------------------------------
47 #------------------------------------------------------------------------------
49 # getNumberedLine dictionary entry
50 # Prints dictionary entry line + lineno
52 grep -n "^[ \t]*$2[ \t]" $1 |
grep -v '^//' |
head -1
55 # getLine dictionary entry
56 # Prints dictionary entry line (without lineno)
58 getNumberedLine
$1 "$2" |
sed -e 's/^[^:]*://'
61 # getRawEntry dictionary entry
62 # Prints value of dictionary entry
64 getLine
$1 "$2" |
sed -e "s/^[ \t]*$2[ \t][ \t]*//"
67 # getEntry dictionary entry
68 # Like getRawEntry but strips " and ending ';'
70 getRawEntry
$1 "$2" |
sed -e 's/^"//' -e 's/;$//' -e 's/"$//'
74 # Prints first item on line
76 echo "$1" |
sed -e 's/[ \t]*\(.*\)[ \t].*/\1/'
80 # setRawEntry dictionary entry newValue
81 # Replaces value of entry
83 oldNumLine
=`getNumberedLine $1 "$2"`
84 lineNo
=`echo "$oldNumLine" | sed -e 's/:.*//'`
85 oldLine
=`echo "$oldNumLine" | sed -e 's/^[^:]*://'`
86 oldKey
=`getKey "$oldLine"`
87 oldVal
=`getRawEntry $1 "$2"`
88 if [ ! "$oldKey" -o ! "$oldVal" -o ! "$oldLine" ]; then
89 echo "setRawStringEntry: entry $2 not found in $1"
92 echo "oldLine=$oldLine"
95 #echo "oldKey=$oldKey"
96 #echo "lineNo=$lineNo"
97 #echo "oldLine=$oldLine"
98 #echo "oldVal=$oldVal"
100 sed -e "${lineNo}s/ ${oldVal}/ $3;/" ${1}_tmp > $1
106 # like getEntry but returns true if boolean is logical true
130 echo "$PROGNAME : getBoolEntry
: Illegal boolean value
$val in dictionary
$1"
136 # newerFile file1 file2
138 latest=`ls -1 -t $1 $2 2> /dev/null | head -1`
139 if [ "$latest" = $1 ]; then
147 # Returns true if pid exists.
149 ps -u $LOGNAME -o 'pid' | fgrep $1 >/dev/null
154 Usage: $PROGNAME [-n] <root> <case> <pid>
156 $PROGNAME -c <root> <case>
158 Tries to end running Foam application at next write or at next time
159 step (-n option). It needs runTimeModifiable switched on in the
160 controlDict. It changes stopAt in the controlDict and waits for the job to
161 finish. Restores original controlDict if
163 - controlDict gets modified (by user)
164 - $PROGNAME gets killed.
166 The -c option clears any outstanding $PROGNAME for the case.
172 # Restore controlDict and clean up
176 echo "$PROGNAME : Restoring controlDict from controlDict_bak.
"
177 if [ -r ${controlDict}_bak ]; then
178 cp ${controlDict}_bak $controlDict
183 echo "$PROGNAME : Exiting.
"
188 #------------------------------------------------------------------------------
192 #------------------------------------------------------------------------------
196 #-- Force standards behaving ps
197 # Get info on all $USER processes
200 UNIX95=a; export UNIX95
211 if [ $# -lt 3 ]; then
216 if [ $1 = '-n' ]; then
221 if [ $1 = '-c' ]; then
224 if [ $# -ne 2 ]; then
231 if [ $# -ne 3 ]; then
239 CASE=`echo $CASE | sed -e 's!/.*!!'` #strip of processorXXX ending
241 #- Pid actually running
242 if [ ! "$CLEAR" ]; then
244 if [ $? -ne 0 ] ;then
245 echo "$PROGNAME : process
$PID not running.
"
250 #- case directory writeable
251 if [ ! -w $ROOT/$CASE ]; then
252 echo "$PROGNAME : $ROOT/$CASE is not writeable.
"
256 #- Controldict writeable
257 controlDict=$ROOT/$CASE/system/controlDict
258 if [ ! -w $controlDict ]; then
259 echo "$PROGNAME : $controlDict is not writeable.
"
264 getBoolEntry $controlDict 'runTimeModifiable'
265 if [ $? -ne 0 ]; then
266 echo "$PROGNAME : runTimeModifiable not true
in dictionary
$controlDict.
"
271 #- Check if another foamEndJob running
273 if [ "$CLEAR" ]; then
274 pidFiles=`ls $ROOT/$CASE/.foamEndJob* 2>/dev/null`
275 for pidFile in $pidFiles
279 echo "$PROGNAME : found
$PROGNAME (pid
$pid) for Foam process
"
282 echo "$PROGNAME : Killing
$PROGNAME (pid
$pid).
"
290 pidFile=$ROOT/$CASE/.foamEndJob${PID}
291 if [ -f $pidFile ]; then
295 if [ $? -eq 0 ] ;then
296 echo "$PROGNAME : found running
$PROGNAME (pid
$pid) for Foam process
"
300 echo " lock
: $pidFile"
301 echo "Remove the lock
if this is not the
case.
"
311 #- Get controlDict entries
316 startTime=`getEntry $controlDict 'startTime'`
317 if [ ! "$startTime" ]; then
318 echo "$PROGNAME : startTime not
set in dictionary
$controlDict.
"
323 writeInterval=`getEntry $controlDict 'writeInterval'`
324 if [ ! "$writeInterval" ]; then
325 echo "$PROGNAME : writeInterval not
set in dictionary
$controlDict.
"
330 stopAt=`getEntry $controlDict 'stopAt'`
331 if [ ! "$stopAt" ]; then
332 echo "$PROGNAME : stopAt not
set in dictionary
$controlDict.
"
337 endTime=`getEntry $controlDict 'endTime'`
338 if [ ! "$endTime" ]; then
339 echo "$PROGNAME : endTime not
set in dictionary
$controlDict.
"
344 echo "$PROGNAME : Read from controlDict
:"
345 echo " controlDict
: $controlDict"
346 echo " writeInterval
: $writeInterval"
347 #echo " startTime
: $startTime"
348 echo " stopAt
: $stopAt"
349 #echo " endTime
: $endTime"
351 echo "$PROGNAME : Making backup of controlDict to controlDict_bak
"
352 cp $controlDict ${controlDict}_bak
353 #- Set up handler to restore controlDict
354 trap restoreDict 2 3 15
356 if [ "$STOPNOW" ]; then
357 setRawEntry $controlDict 'stopAt' 'nextWrite'
358 setRawEntry $controlDict 'writeInterval' '1'
360 echo "$PROGNAME : Changed
in controlDict
:"
361 echo " `getLine $controlDict 'stopAt'`"
362 echo " `getLine $controlDict 'writeInterval'`"
364 setRawEntry $controlDict 'stopAt' 'nextWrite'
366 echo "$PROGNAME : Changed
in controlDict
:"
367 echo " `getLine $controlDict 'stopAt'`"
372 #- Just to make sure time has changed
377 #- Give bak a later date
378 touch ${controlDict}_bak
380 #- Loop a while to give NFS time to update
381 if newerFile ${controlDict} ${controlDict}_bak; then
382 echo "$PROGNAME : controlDict newer than controlDict_bak.
"
383 echo "$PROGNAME : Waiting
for file dates to get updated.
"
386 while newerFile ${controlDict} ${controlDict}_bak
388 if [ $iter -ge 120 ]; then
389 #- 120*5 sec = 10 mins passed. Give up
390 echo "$PROGNAME : File
date not yet ok after
10 mins. Giving up.
"
393 #- Give _bak a later time
394 touch ${controlDict}_bak
396 #- Give nfs some time to update time on controlDict.
399 iter=`expr $iter + 1`
404 #- Start waiting until:
405 # - pid finished. Restore controlDict.
406 # - controlDict modified. No restore.
407 # - controlDict_bak removed. No restore.
409 echo "$PROGNAME : Waiting
for Foam job
$PID to finish ...
"
415 if [ ! -r ${controlDict}_bak ]; then
416 echo "$PROGNAME : ${controlDict}_bak dissappeared. Exiting without restore.
"
420 if newerFile ${controlDict} ${controlDict}_bak; then
421 echo "$PROGNAME : ${controlDict} modified externally. Exiting without restore.
"
426 if [ $? -ne 0 ] ;then
430 #echo "Foam job
$PID still running ...
"
433 #- Dictionary restore
436 #------------------------------------------------------------------------------