13 Replies Latest reply: Feb 27, 2012 11:41 PM by ChangeAgent
ChangeAgent Level 1 Level 1 (140 points)

I have a Mac Mini that I use as a home server. I have the basic energy saver scheduler turned on, but sometimes a application is hanging and as such preventing a shut down. 

 

I want to schedule a force quit of every application that is hanging.  I would have this run from cron a few minutes before the shut down.  Or even have cron shut down at that point.

 

I thought I start gently by Quitting all open applications with this.

 

 

tell application "System Events" to set the visible of every process to true

set white_list to {"Finder"}

try

          tell application "Finder"

                    set process_list to the name of every process whose visible is true

          end tell

          repeat with i from 1 to (number of items in process_list)

                    set this_process to item i of the process_list

                    if this_process is not in white_list then

                              tell application this_process

                                        quit

                              end tell

                    end if

          end repeat

 

 

How can I add a bit that would kill any other applications so that when the energy saving panel comes to shutting down, it can.


MBP, MM, MBP - 10.6 + Windooz XP on a hard partition
  • fane_j Level 4 Level 4 (3,660 points)

    Rather than this method, have you tried simply,

     

    on run

              tell application "System Events" to shut down

    end run

     

    I don't know if this is cancellable by apps. If it is, then shutdown from a do shell script should shut down the system, no ifs, ands, or buts.

  • ChangeAgent Level 1 Level 1 (140 points)

    fane_j wrote:

     

    Rather than this method, have you tried simply,

     

    on run

              tell application "System Events" to shut down

    end run

     

    I don't know if this is cancellable by apps. If it is, then shutdown from a do shell script should shut down the system, no ifs, ands, or buts.

    thanks, this would not work as Applications that hang will cancel it. 

     

    Shell scripts is an option but a bit hard or?

  • fane_j Level 4 Level 4 (3,660 points)

    ChangeAgent wrote:

     

    Shell scripts is an option but a bit hard or?

    Not a shell script, but AppleScript's do shell script command. Like this

     

    on run

      do shell script "shutdown -h now"

    end run

     

    I can't test it, 'cos I can't shut down now. If the account in which you're running it doesn't have admin privs, you might need to do (with due caution about hard-coding your name and pass)

     

    on run

      do shell script "shutdown -h now" user name "your_user_name" password "your_password" with administrator privileges

    end run

     

    All the details about 'do shell script' are in Apple's TN 2065.

     


  • ChangeAgent Level 1 Level 1 (140 points)

    yes understood, but maybe no clearly answered from my site. 

     

    I will test it later but I am quite sure it will hang on a hanging Application, just as the control panel command does now.

     

     

    Hence me looking for a way to shut down hanging applications first and gently.  And I do not want to do a kill all.  unhealthy.

  • R C-R Level 6 Level 6 (14,770 points)

    I suggest trying enclosing the "quit" statements inside "try" clauses with "on error" clauses to trap the failed quits & prevent them from halting your 'gentle' quit section. You could also include statements to compile a list of the apps that won't quit & maybe try to handle them separately after the 'gentle' section is done. You also may be able to do some housekeeping like writing out files to the disk before the forced shutdown to minimize the undesirable effects. There should be shell commands to do this but you will have to get help with that from someone else.

     

    I'm not sure this will work but the 'try' construction is something you should check out for anything that might hang or halt your scripts.

  • BobHarris Level 6 Level 6 (14,215 points)

    "shutdown -h now" stops for  no one, assuming you can run it with root privs

  • R C-R Level 6 Level 6 (14,770 points)

    Which is why he says it is unhealthy.

  • fane_j Level 4 Level 4 (3,660 points)

    ChangeAgent wrote:

     

    I am quite sure it will hang on a hanging Application

    If it does, I'll eat my hat.

    I do not want to do a kill all.  unhealthy.

    You'll have to elaborate on that.

     

    The alternative is to get a list of all running processes from System Events, and tell each one to quit, in a try statement, as suggested above. Then repeat the procedure, this time force-quitting (ie, killing) those which did not quit the first time. There are all sorts of issues you'll have to deal with. In the end, nothing is as robust as shutdown -- it's the reason shutdown was created in the first place.

  • rccharles Level 5 Level 5 (5,685 points)

    Hence me looking for a way to shut down hanging applications first and gently.  And I do not want to do a kill all.  unhealthy.

    Well, what are you going to do if the app doesn't respond to quit?  If the app doesn't repond to quit, it is messed up and your going to have to force quit the app.

     

    here is the terminal command to force quit an app. Change Finder to you app name.

    killall "Finder"

     

    There are a bunch of other signals you can send to the app.  Here is the list:

    mac $ killall -l

    HUP INT QUIT ILL TRAP ABRT EMT FPE KILL BUS SEGV SYS PIPE ALRM TERM URG STOP

    TSTP CONT CHLD TTIN TTOU IO XCPU XFSZ VTALRM PROF WINCH INFO USR1 USR2

    mac $

    I haven't tried any of them.

     

    So your applescript doesn't hang up look at the timeout statement page 210 of

    AppleScript Language Guide pdf download the pdf file

  • ChangeAgent Level 1 Level 1 (140 points)

    Thanks R C-R,

     

    I am no that good in scripting but will give it a try.

  • ChangeAgent Level 1 Level 1 (140 points)

    BobHarris wrote:

     

    "shutdown -h now" stops for  no one, assuming you can run it with root privs

    OK good to know!

  • ChangeAgent Level 1 Level 1 (140 points)

    thanks fane_j

  • ChangeAgent Level 1 Level 1 (140 points)

    good info rccharles, bot sure I mange with my skill level but will give it a go.