How to Restart in 10.4.11 with Applescript and prevent hanging

Tom,

hanging means the application is frozen and doesn’t respond to any user action.
The script doesn’t quit apps, which run normally

Good morning everybody and thanks for chipping in, I was away and it surely has been busy.

I like to test this script (not that I do not believe any of you ☺ ) so how do I let a applications ˜hang’?

I made a script that can not be executed but that does not seem to do the trick as it does not really make it hang. So how to flip-out a programme so I can test the script? Any ideas?

Thanks

thanks Jacques

yes a good solution. any document open will however not be saved. however you are now exposing your admin password too anybody that can read the script, so will need to be saved as a run only script for a bit of security.

In a Terminal window try kill -STOP PID to “hang” a process. Use kill -CONT PID to unhang it. Respectively, these send the SIGSTOP and SIGCONT signals to the process which cause the kernel to stop and to resume scheduling it. Only processes that are scheduled are ever allowed to execute. This should effectively make the process appear hung. Get the PID with either the ps command or by scripting System Events: tell application “System Events” to get unix id of application process “Preview”.

If you stop the enough things (Dock, Terminal, Finder, SystemUIServer (Spotlight), and Script Editor, all other launcher applications, etc.) it may be difficult to resume anything without doing so via a remote login (the only other alternative being a forced restart). I suggest limiting your testing to suspending and resuming some app where the effects will be innocuous, perhaps Preview, or a browser that you do not regularly use.

If you cannot use Terminal, I think Bokeh has the same ability to send SIGSTOP and SIGCONT to application processes (probably with a bit of eye candy, too). I have never even downloaded it though, so I am just guessing. It says it requires 10.5, so that may not work for the original poster.

Edit: Add info about Bokeh requiring 10.5.

Thanks Chrys, I tried this and it does indeed stop Preview, it hangs nicely.

James, Stefan.

I have managed to hang a program, but the script does not kill it. am I doing anything wrong?

And doing further testing it also quits programs that are not hanging.

any idea as to what is going on here? am I misunderstanding something?

Thanks

Sorry, I was mistaken.
Hanging apps is not the same as processes with state Z(ombie) in ps
You could check all apps by sending an Apple Event and wait for a controlled timeout error


property shortTimeout : 5
tell application "System Events"
	set hungAppProcesses to {}
	repeat with oneProcess in (get application processes whose background only is false)
		try
			with timeout of shortTimeout seconds
				get title of oneProcess
			end timeout
		on error number n
			if n is -1712 then set end of hungAppProcesses to comtents of oneProcess
		end try
	end repeat
	hungAppProcesses
end tell

Thnaks Stefan

if n is -1712 then set end of hungAppProcesses to comtents of oneProcess

it hangs here and I get the following message:

System Events got an error: Can’t make comtents of application process “Preview” into type reference.

now this is before I crashed a application.

the same happens if I ‘hang’ an application.

any suggestions?

FYI I am hanging ‘Preview’.

sorry misprint

it’s contents not comtents

Thanks Stefan

I hung Preview and when running the script I now get as a result:

{application process “Preview” of application “System Events”}

but it does not quit Preview.

Indeed it does not, it gathers only the hanging apps
add these lines to the end the script


 repeat with i in hungAppProcesses
	quit application i
end repeat

You can also include the quit command it in the error handler,
but with this version you can see, which apps are affected

Stefan,

I did ad the line and have now:

property shortTimeout : 5
tell application "System Events"
	set hungAppProcesses to {}
	repeat with oneProcess in (get application processes whose background only is false)
		try
			with timeout of shortTimeout seconds
				get title of oneProcess
			end timeout
		on error number n
			if n is -1712 then set end of hungAppProcesses to contents of oneProcess
		end try
	end repeat
	hungAppProcesses
end tell

repeat with i in hungAppProcesses
	quit application i
end repeat

the scriopt now hangs on:

quit application i

I get the message:

Can’t get application (application process “Preview”) of «script».

sorry to be a pain, but I am lost.

Next try, the apps have to be killed in the terminal, because a hanging app doesn’t respond to the quit event


property shortTimeout : 5
tell application "System Events"
	set hungAppProcesses to {}
	repeat with oneProcess in (get application processes whose background only is false)
		try
			with timeout of shortTimeout seconds
				get title of oneProcess
			end timeout
		on error number n
			if n is -1712 then set end of hungAppProcesses to unix id of oneProcess
		end try
	end repeat
	hungAppProcesses
end tell
repeat with i in hungAppProcesses
	do shell script "/bin/kill " & (i as Unicode text)
end repeat

Sorry Stefan, but no cigar, I have Preview hanging and running the script does not do it.

I also tried terminal with /bin/kill PID and Preview does not respond to it.

hm,

I’ve tested the last version with Preview.app and it worked

hm indeed.

so to recap I used Terminal command kill -Stop PID to hang Previeuw. if I select it it offers me the choice to force Quit so it is hanging.

Than I run your script and nothing happens. Am I doing something wrong or different from what you do? Or?

I am testing this under OS X.4.11 btw.

I did


tell application "System Events" to set pid to the unix id of process "Preview" as Unicode text
do shell script "/bin/kill -STOP " & pid

to force the process to hang and then I run the script.
It waited the 5 seconds and killed the app. The list hungAppProcesses contained the pid of Preview.app
After the second run of the script the list hungAppProcesses was empty

nope. repeated your steps and it does not quit Preview.

Stefan,

did some experiemens this morning and Preview does not respond to a simple script like

do shell script "killall 'Preview'"

I suppose the script is OK, but I got an other problem.

I also tried it with hanging Textedit, same story.

anybody got a clue what could be going on?