I’m working on a script that launches a background service (OpenOffice.org as a service) and then runs a command. For the script to work correctly, it needs to wait until the service is available before running the next command. Because OpenOffice.org-as-a-service is buggy under OS X, I also need to test whether the service started successfully. I test whether the service is running correctly (it uses port 8100) and also listening by running “netstat -an | grep ‘8100 . LISTEN’”.
I’ve put together this script that waits for the service to start running, but gives up after 5 seconds. It seems to work correctly, but it looks like a horrible kludge, and I hope an expert might be willing to tell me how to make it more elegant and efficient. Here it is:
set listening to false
set num to 0
repeat until (listening is true) or (num is greater than 10)
delay 0.5
set listening to true
set num to num + 1
try
do shell script "netstat -an | grep '8100 *.* LISTEN'"
on error
set listening to false
end try
end repeat
display dialog "Listening: " & listening
Any improvements will be gratefully received.