not waiting...why?

My shell script is not waiting. I right click on a file and say convert it to whatever and it runs this script. It converts it using Compressor but I want it to wait until it is 100% done before moving on and it is not waiting. I have tried to put it in the background and using "wait". I have tried to check for the file existence of the new file (new file shows up prematurely...before the entire encoding has been done so this is out). Anyone else recommend anything else?

#! /bin/sh
exec > /tmp/keith 2>&1
set -x
date
open /Applications/Utilities/Batch\ Monitor.app 

/Applications/Compressor.app/Contents/MacOS/./Compressor \
	-clustername 'This Computer' \
	-batchname 'FLV-480X360' \
	-priority low -jobpath "$1" \
	-settingpath '/Users/keith/Library/Application Support/Compressor/FLV - 480 X 360.setting' \
	-destinationpath "$2"

if [ $? != 0 ]
then
	echo "failure from Compressor...$?" | mail -s "$3 failed" myemail@email.com
	echo "Compressor failed"
	open /tmp/keith
	exit 1
fi 

if [ -s $2 ]
  then
   flvtool2 -U "$2"
open -a /Applications/QuickTime\ Player.app "$2"

open /System/Library/CoreServices/Finder.app "$4"
fi

echo "FLVTOOL2 returned $?"

if [ $? != 0 ]
then
	echo "failure from FLVTOOL2...$?" | mail -s "$3 failed" myemail@email.com
	echo "FLVTOOL failed"
	open /tmp/keith
	exit 1
fi 

Have you tried changing the tests for 0 from != to -ne ?
So instead of if [ $? != 0 ] use if [ $? -ne 0 ]

S.

I have simplified it to just open the file after conversion. I think the problem is the script thinks it is done when the file is created like 30 seconds into encoding or so. However, the file is not 100% complete. In the script below the QT player never opens.

I'm not sure what to do next...any suggestions?

#! /bin/sh
exec > /tmp/keith 2>&1
set -x
date

/Applications/Compressor.app/Contents/MacOS/./Compressor \
	-clustername 'This Computer' \
	-batchname 'FLV-480X360' \
	-priority low -jobpath "$1" \
	-settingpath '/Users/keith/Library/Application Support/Compressor/FLV - 480 X 360.setting' \
	-destinationpath "$2" &&
wait

if [ $? -ne 0 ]
then
	  open -a /Applications/QuickTime\ Player.app "$2"
fi

This is my log file:

+ date
Fri Nov 14 12:29:20 EST 2008
+ /Applications/Compressor.app/Contents/MacOS/./Compressor -clustername 'This Computer' -batchname FLV-480X360 -priority low -jobpath /Users/keith/Desktop/test/file.mov -settingpath '/Users/keith/Library/Application Support/Compressor/FLV - 480 X 360.setting' -destinationpath /Users/keith/Desktop/test/file.flv
2008-11-14 12:29:21.971 Compressor[456:10b] owner is: keith
2008-11-14 12:29:21.994 Compressor[456:10b] <jobID E4477B97-0E68-4625-97EE-125F1D27843A /> <batchID 67A9FB90-2187-4442-A9CA-CB964F963BDD />
+ wait
+ '[' 0 -ne 0 ']'

In your script I notice a double && which should be just ambersand. Further, the test should now read [ $? -eq 0 ] instead of [ $? -ne 0 ] and then it will launch the player I think.

If then still Quicktime launches before the conversion is finished I suggest you have a look at /Applications/Compressor.app/Contents/MacOS/./Compressor, which I suspect is a script in itself that may be launching the actual program in the background without waiting for it to finish. Perhaps you could then use wait loop until the creation of the file is finished (wait for a feature of the new file to change, e.g. test for non-zero length, maybe?)

S.

Ya I put only one & and changed it to [ $? -eq 0 ] and QT pops up once the file is created but not when the file is complete. If you check the info of the file when converting it keeps adding to the file size. So I'm not sure how I would know when it is 100% done. I tried to open the Compressor script but of course it is all mumbo jumbo.

Any suggestions?

Perhaps you could keep checking the file every few seconds to see if the files size stopped changing, e. g. like so:

NEW=$(ls -s "$2")
while [[ "$NEW" != "$OLD" ]]; do
  sleep 5
  OLD=$NEW
  NEW=$(ls -s "$2")
done

S.

I think it works now but I'm totally confused as to why it is behaving this way. The script below works...or at least once the file has encoded completely the QT movie opens...about 15 seconds or so later. That is weird to me. Another weird thing is before the flv would show up even though the file was not 100%. Now the file does not show up until 15 seconds or so after the file has completed.

Note: Scrutinizer you initially recommended 5 seconds to sleep. I changed it to 15 because QT was opening early like before. When I check the log file it said the two values did equal each other. I assume maybe the file took a 5 seconds break or something so I increased it to 15 and it fixed it.

#! /bin/sh
exec > /tmp/keith 2>&1
set -x
date

/Applications/Compressor.app/Contents/MacOS/./Compressor \
	-clustername 'This Computer' \
	-batchname 'FLV-480X360' \
	-priority low -jobpath "$1" \
	-settingpath '/Users/keith/Library/Application Support/Compressor/FLV - 480 X 360.setting' \
	-destinationpath "$2" &
wait

if [ $? -eq 0 ]
then

NEW=$(ls -s "$2")
while [ "$NEW" != "$OLD" ]; do
  sleep 15
  OLD=$NEW
  NEW=$(ls -s "$2")
done
	  
if [ "$NEW" == "$OLD" ]
then
open -a /Applications/QuickTime\ Player.app "$2"
fi

fi

Indeed, the 5 seconds was only a suggestion, good thing you fine-tuned the interval so that it works reliably. I am not sure, is it OK like this or are you still confused about something?

It is a great thing to have a log file or else I never would have thought about that.

I'm confused to why the file is not created right away like it was before (even though it was not 100%) anymore. How does what we added change the file being created?

Why does the file take 15 seconds or so to show up after my program says it is complete? I know you probably are not familiar with the software but from my understanding of your code...

if the file is there then it gives an exit status of 0 and it goes into while loop which keeps checking the file until the old and new filesizes match. I don't see how the checking of the file has anything to do with the Compressor and the file not showing up or the delay of it showing up after completion.

Last thing, when the script is run a beach ball comes which restricts me clicking on things like my desktop items or whatever. This never happens with my other scripts. Do you see any reason why this is happening? I thought maybe it is because of the "&" in the script but I removed them and the wait and it still does it. Any ideas?

I think the compress script is calling yet another program which it runs in the background. It does not wait for it to be finished however, and exits instead.
So when the compress script finishes with exit code 0, the actual program is still running in the background and the file is being created and is growing in size.

What we added does not change the file being created, but 'watches' it to see if the growing of he size of the file has stopped, i.e. two consecutive file size samples are the same. If the size no longer increases the script exits the wait loop and goes on to start quicktime.

I presume the beach ball means that your system is using 100% CPU while creating the file?

O.K. I solved the beach ball problem. I have OMCEdit which allows me to do the context menus and get the variables and that was running the script in silent mode(popen)...I changed it to do Shell Script and it fixed it.

Thank you so much for your help. I have been trying to solve this for a couple months now and I couldn't find any solution. You are a real asset to this forum. Thanks so much.