Help with a watchdog script please

Hi,

I am relatively new to applescript but I am learning. I have been searching around for something that would fit my needs and I can’t seem to put it all together.

I am trying to check the status of an application once every 15-30 seconds to see if it locked up. The problem is that the only way to tell if the application is locked up is to look at activity monitor, the dock icon or the menu and look for force quit.

While the application is frozen, the logging for the app will continue to run in the background so I cannot do a periodic check of the log files. Activity monitor will show up as App(Application not responding), and the display will not refresh without restarting the application via Force Quit.

I have been digging around the forums and nothing I have seen really seems to be working for me.
-I have tried to monitor the app by checking the state in PS but it does not report the state as dead/zombie “Z” when it is crashed.
-The app does not report an abnormally high memory or cpu usage prior to crashing

So far, I can successfully check to see if the application is running, and restart the application if it is not. But checking to see the status of the app is eluding me.

Can anyone please help me figure out how to monitor this application properly please?

Thanks!

Sometimes this works… first you find an applescript command that the program will respond to. You want to try and find one that is not intrusive so it doesn’t bother you every 30 seconds. The same applescript command cannot work when the application is crashed. Thus you have a way to check for a crashed app.

Here’s an example of an intrusive applescript command which works on any application. You haven’t mentioned anything about your “trouble” application so I can’t give you any suggestions on a command that might be appropriate for this.

-- First we use a try statement when issuing the applescript command
-- in case the application errors immediately when it is crashed
--
-- Second we use "ignore application responses" in case the application doesn't error when it is crashed
-- because we don't want to be waiting indefinitely for a response from a crashed application
try
	ignoring application responses
		tell application "TextEdit"
			activate
		end tell
	end ignoring
on error
	error "TextEdit is crashed!"
end try

-- because we didn't wait for a response we pause briefly to ensure
-- the application has time to come to the front if it's not crashed
delay 1

-- now we check to see if the application responded to our applescript command
tell application "System Events"
	set frontApp to name of first process whose frontmost is true
end tell

if frontApp is not "TextEdit" then error "TextEdit is crashed!"

Hi, Hank – I got the impression that the OP was tracking a background app so it won’t come to the front.

Probably Adam, but even so it’s possible that the background app takes commands even if it’s a shell program. For example launchd is a background process but you can query it and thus applescript could be used to trap errors and thus do something about it. So my example was more about showing how to do that without knowing exactly which command could be used as the trigger.

Thanks for the responses, I will try to see if the suggested script can work for my needs.

The application that I am monitoring is a java based app that runs as a full screen information screen (like a kiosk). Even when this app is not responding correctly it is still the only visible app, it simply stops getting periodic updates from a central server.

if the application has an unique bundle identifier a launchd agent with keepAlive key, SuccessfulExit false and runAtLoad true might work.

I don’t mean to hijack, but I’d like a quick clarification- can a Java based application even HAVE an Applescript dictionary and perform commands?

Now there’s a great idea.

I already use a launchd item to start the application at startup, as well as to check if the application is running every 30 seconds.

Initially, I used this launchd item:

But would this work out for me instead?

Thanks so much!

I tried out the launchd task listed above, but i am not able to load the application using the CFBundleIdentifier for some reason.

try to use the path to the executable as program argument
instead


  <key>ProgramArguments</key>
    <array>
        <string>CFBundle Identifier</string>
    </array>

write


  <key>Program</key>
    <string>/path/to/executable</string>

Thanks, I will give that a run.