Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

run process within script to continually check for status

Status
Not open for further replies.

gotchausa

Programmer
Jan 24, 2002
64
CA
Using ksh, how would I do the following within a script:

1. Check for existence of a file
2. If file exists, do some other tasks. At the same time as these tasks are being done, continually rpt step 1. As soon as step 1 detects that file does not exist, quit from script immediately. If and when all tasks are done and file still exists, remove it.

THx.
 
Hi mpv,

I would do something along these lines.
The "Other tasks" that you need to do will go in there own shell script e.g.

---------------------------------------------------
#!/bin/ksh

Loops=0
while [ $Loops -lt 5 ]
do
sleep 10
Loops=`expr $Loops + 1`
TimeGone=`expr $Loops * 10`
echo "Time running is $TimeGone secs" >> ./Loops.out
done

rm -f ./blah.txt #remove file here after other stuff
#finishes
--------------------------------------------------------

The above script is executed in the background by a controlling loop that continually monitors whether the file is still there. As soon as it dissappears it will kill the above script (if it's still there and terminate)
e.g.

--------------------------------------------------------
#!/bin/ksh

if [ -f ./blah.txt ]
then
./test.sh & #start other script in background
Process=$! #capture PID

Flag=1

while [ $Flag -eq 1 ]
do
sleep 1
if [ ! -f ./blah.txt ]
then
#file's dissappeared
kill $Process > /dev/null
Flag=0 #exit loop
fi
done
fi

---------------------------------------------------------

Hope this is what you wanted.

MB.
 
Thanks a lot! Yes, this is along the line of what I need, I'll try it out and hope it works correctly...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top