Hanging shell process

I have an applescript that runs the unix “host” command in a shell to generate a listing of every dns entry in my companies domain. Recently, the host command has started to hang, it just gets stuck and never reports anything back the the applescript that initiated the process. The applescript then appears to be hung, I never get a timeout message. The users force quit the script, but that leaves the host command process running (you can see it in activity viewer or top).

Does anyone know of a way to put a time out on a shell command, so the script can recover and kill any stuck processes? I have tried putting the do shell script command in try and with a timeout of blocks, but they don’t seem to make a difference.

Below is the subroutine that creates my DNS listing file, for reference.


createDNSfile()

on createDNSfile()
	
	set CreateFile to "date \"+%m-%d-%y%n%H:%M:%S\" > ~/Library/Preferences/com.mycompany.dnslist.txt; host -l mycompany.com  >> ~/Library/Preferences/com.mycompany.dnslist.txt"
	--set blah to (display dialog "Command:" default answer CreateFile)
	
	set createfileresult to do shell script CreateFile
	-- display dialog "DNS Table has been updated" buttons {"OK"} default button 1
end createDNSfile

I would try to execute the shell script as a background process. Then it should be possible to regulary check if it’s still running in an idle loop in your AppleScript and kill it if necessary.

how do I initiate the shell script as a background process from within Applescript?

A brief bit of research later, (http://www.ee.surrey.ac.uk/Teaching/Unix/unix5.html) I assume you mean put an & at the end of the command, like this:

host -l mycompany.com >> ~/Library/Preferences/com.mycompany.dnslist.txt &

I can then run:

ps axww | grep -v grep | grep ‘host -l’ | awk ‘{print $1}’ | xargs kill -9

as a shell script and that will kill the process if it is stuck still running.

thanks for the suggestion - I still am learning the shell (does anyone really know unix ever?)

Don

I have tried to make this work, but apparently the “do shell command” in applescript waits until the shell session is completely closed before it returns command to the applescript. If you run the simple script below using the unix sleep command, you will see that the applescript dialog only appears after the number of seconds specified after sleep.

I tried using sh -c before the command, but then sleep is never invoked (I don’t see it appear in activity monitor).

Any help would be appreciated.


set theCommand to "sleep 60 &"
--display dialog "Command to cut and paste:" default answer theCommand
do shell script theCommand
display dialog "Applescipt can execute commands"

ok, I am getting a bit paranoiind becuase I am having this conversation mostly with myself, but I want to post this for the record when someone else is trying to figure this out.

This tech note from Apple explains how to do this (and a lot more about do shell script):

http://developer.apple.com/technotes/tn2002/tn2065.html#TNTAG5

below is a working script sample:


set theCommand to "sleep 60 &> /dev/null & echo $!"
--display dialog "Command to cut and paste:" default answer theCommand
do shell script theCommand
set pid to the result
display dialog "Applescipt can execute commands " & pid
delay 5

do shell script "kill " & pid