How can I check the application if responsive??

Hi All,
I am a new hand of Applescript. An application I have to work 24 hours. But it’d crash sometimes(no response,CPU usage over 100%…).
So I try to auto-detect every 30 mins using Applescript.I would detect application “Osirix” if responsive …If no response then close the application “Osirix” and activate “Osirix” again.
I refer some old hand article to check state…but it’s always pass the checking even the Application “Osirix” is responsive…
Dose any guys can give me a hand.thanks a lot…


delay 180
 
repeat
set The_app to "Osirix"
set state to ""
tell application "System Events"
try
set pid to the unix id of process The_app as Unicode text
set state to paragraph 2 of (do shell script "ps -p " & quoted form of pid & " | awk '{ print $3 }'")
end try
end tell
 
if state = "Z" then
do shell script "kill " & pid
 
tell application The_app to quit
tell application The_app to activate
 
else
display dialog "Osirix is working" giving up after 3
end if
delay (1800)
end repeat

OS X Mountain Lion (10.8.2)

Hello.

I changed the parameter for the ps command, enforced your kill, (it is a zombie process) and turned your script into an idle handler, which you should just save as an app, and make it run. (I have tossed in a run handler so that you can see that it actually does!

on run
	tell me
		activate
		display dialog "I am up and running!" giving up after 3
	end tell
end run

on idle
	local firstTime, The_app, state, pid
	try
		if firstTime = false then set firstTime to true
	on error
		delay 180
		set firstTime to true
	end try
	
	set The_app to "Osirix"
	set state to ""
	tell application "System Events"
		try
			set pid to the unix id of process The_app as Unicode text
			set state to paragraph 2 of (do shell script "ps ax " & quoted form of pid & " | awk '{ print $3 }'")
		end try
	end tell
	
	if state = "Z" then
		do shell script "kill −9 " & pid
		delay 10
		try
			tell application The_app to quit
		end try
		tell application The_app to activate
		
	else
		tell me
			activate
			display dialog "Osirix is working" giving up after 3
		end tell
	end if
	return 1800
end idle

Dear McUsr,
Thanks a lot! :smiley:
I am going to try ASAP.thanks your kindly help ^^

Davidtt

Hello, if it doesn’t work, then try removing the braces from the idle handler. I have removed them from the script above.