How to Restart in 10.4.11 with Applescript and prevent hanging

have been fighting with this for a while and have asked before, but still have no working solution. (partly because I am a beginner at this)

I need a script to reboot and to shut down a remote server all by its self.

I have

ignoring application responses
tell application “System Events”
restart
end tell
end ignoring

Problem with scripts like this is that they have no error trapping in them and I would not know how to do this. Any tips?

Also I would like it to execute at a certain time and not use iCal (see under).

Alternatively I would suspect one could also do this with a shell script and the shutdown command.

If I use System Events, I don’t need to authenticate, and it’s just like using restart from the Apple Menu, which means the restart can be blocked by an application that won’t quite. Which it does.

Is there anybody who can help me out here? I suspect it can be done in a shell script. But this is at present beyond my skill. Of course all this needs to happen at a specific time, I do not want to use iCal as this is one of the programmes that hangs on me at shut down and blocks the process regularly and I have not been able to fix that either.

Thanking you for thinking with me.

Well one thing you can do is force kill any open applications… here is an example

set exclude_apps to {"Finder"}
	tell application "System Events" to set the_apps to name of processes whose background only is false
	repeat with the_app in the_apps
		if exclude_apps does not contain the_app then
			tell application the_app to activate
			try
				do shell script "/bin/kill `ps ax -w | awk /'" & the_app & "'/ | awk '{print $1}'`"
			end try
		end if
	end repeat

Thanks James

This is a kill script right? And so would kill all application even if they have open processes.

Or is this like a force quit script/effect?

I am asking to find out if this is ‘dangerous’ to shut stuff down this way.

sorry if this is a dumb question, but I am pretty new to all this stuff.

Thanks

the example I posted will basically check for any open application whose background is not true so it will find apps like ichat, photoshop, etc… but not system process. Then after that you would stick your restart statement.

This is the same approach I used in a system maintenance script I wrote and have been using for a few years.

set exclude_apps to {"Finder"}
tell application "System Events" to set the_apps to name of processes whose background only is false
repeat with the_app in the_apps
	if exclude_apps does not contain the_app then
		tell application the_app to activate
		try
			do shell script "/bin/kill `ps ax -w | awk /'" & the_app & "'/ | awk '{print $1}'`"
		end try
	end if
end repeat

-- do whatever here

tell application "System Events" to restart

OK thanks James,

so it will not force quit a application if it hangs?

so could we ad this to it in some way?

and since you mentioned your ‘restart part of the script’ would you mind to share this with me?

thanks

Ahh I misunderstood your question I think… yes the kill statement is not graceful ie if the application hangs or the user has unsaved documents it will quit it regardless.

In terms of the restart I use the method that you implied earlier… telling system events to restart

mm so no other elegant solution than.

Well I’m sure something more elegant can be thought up, but since the solution fit my needs I never explored something more elegant.

Now in terms of running this not using iCal I would suggest you investigate using Launchd. Also in terms of applications being open if this is running on a server there really shouldn’t be any apps open anyways should there? Or certainly not any that would interfere with a restart.

Is a bit over my head so not tried as of today, will look in to it, any pointers?

this is correct, till one hangs which happened the other day and I was 8000 miles away so could not do a thing. so that is why I started to think about other solutions.

Is it really necessary to restart the computer?
With the ps command you can also watch the state of any process (e.g Run, Sleep, Idle, Dead and some other else)
If it was dead you could explicitly kill this process and relaunch the app.

well the need arose form the fact that we shut the server down between midnight and 6AM. so we used the control panel to do this and than a programme hung so if this happens we can not access the computer with ARD or Chicken at all and only do a restart (hard) if you are there. hence the idea to create a script that does the restart or shut down in this case.

In this sort of an instance is the machine still answering to SSH?

If a restart is not obligatory, you could run periodically a script like this.
It checks the state of every running application. If the state is dead (Z), the app will be killed.
A relaunch line could be added.


tell application "System Events" to set theApps to name of processes whose background only is false
repeat with oneApp in theApps
	set pState to do shell script "/bin/ps -arxo state,comm | /usr/bin/grep " & oneApp & " | /usr/bin/cut -c 1"
	if pState contains "Z" then -- if the application hangs, kill it
		tell application "System Events" to set pid to the unix id of process appname as Unicode text
		do shell script "/bin/kill " & pid
	end if
end repeat

Cool Stefan, and thanks, learning all the time. how would one ‘ad a relaunch line’? Not sure ho to script this.

try


.
end if
delay 5 -- security delay
activate application appName
end repeat
.

Sorry if I am seem think,

does this activate all applications? or do I replace “appNmae” with the name of a specific application?

sorry, there are two mistakes in the script above.
Here is the right one


tell application "System Events" to set theApps to name of processes whose background only is false
repeat with oneApp in theApps
	set pState to do shell script "/bin/ps -arxo state,comm | /usr/bin/grep " & oneApp & " | /usr/bin/cut -c 1"
	if pState contains "Z" then -- if the application hangs, kill it
		tell application "System Events" to set pid to the unix id of process oneApp as Unicode text
		do shell script "/bin/kill " & pid
		delay 5 -- security delay
		activate application oneApp
	end if
end repeat

only the killed apps will be relaunched

Did you try the most recent one of post #17?
My intention was to avoid the restart by killing the hanging apps

It quits only apps which are hanging (not responding)

Tom,

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