Script to shutdown mac

26,432

Solution 1

Could you try a simple applescript, which goes something like this...

tell application "System Events"
shut down
end tell

See if it works, and then you can make it run through Automator at certain time, etc.

Solution 2

my solution (somwhat late). Just a bash script with apple in it:


#!/bin/bash
# OK, just shutdown all ... applications after n minutes
sudo shutdown -h +2 &
# Try normal shutdown in the meantime
osascript -e 'tell application "System Events" to shut down'

I also edited the /etc/sudoers (and /private/etc/sudoers) file(s) and added the line: ALL=NOPASSWD: /sbin/shutdown

Always worked for me for an assured shutdown (knock knock ;-) )

Solution 3

This should do:

do shell script "shutdown" with administrator privileges

If you want to pass the admin password from key chain, with no prompt:

do shell script "shutdown" with administrator privileges password "password here"

But do not store the admin password in clear anywhere. Instead use the keychain access.


Alternatively you could kill all user processes, via:

do shell script "kill -9 -1"

This however would also kill your own Applescript process, preventing it from requesting the shutdown/restart afterwards.

Either way you're playing with fire, when using sudo or kill.

Share:
26,432

Related videos on Youtube

blue-sky
Author by

blue-sky

scala :: java

Updated on July 05, 2022

Comments

  • blue-sky
    blue-sky almost 2 years

    I'm trying to automate the shutdown of my mac, I've tried the scheduled shutdown in energy saver and I wanna sleep but these don;t seem to work. VLC player runnign seems to prevent the shutdown. I think I need a script to forcefully shutdown the mac regardless of of what errors may thrown to screen by various programs running.

    Thanks

    Ok,

    This is the applescript code im using to shutdown may mac. I've added it as an iCal event thats runs nightly.

    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
                do shell script "killall \"" & this_process & "\""
            end if
        end repeat
    on error
        tell the current application to display dialog "An error has occurred!" & return & "This script will now quit" buttons {"Quit"} default button 1 with icon 0
    end try
    
    
    
    tell application "System Events"
        shut down
    end tell
    
  • notthetup
    notthetup about 13 years
    Or wait.. is the problem that such a call gets cancelled due to all the other programs which cancel the sleep?
  • blue-sky
    blue-sky about 13 years
    sudo reboot - does that not restart the mac ?
  • Regexident
    Regexident about 13 years
    Fixed. Use "sudo halt" for sleep, "sudo restart" for restart or "sudo shutdown" for shutdown.
  • Nicholas Riley
    Nicholas Riley about 13 years
    ...you don't need sudo with "with administrator privileges", you're already root. Also it's reboot for restart. halt is a less careful shutdown, not sleep, which is pmset sleepnow.
  • Regexident
    Regexident about 13 years
    Thanks fixed. My journeys in Applescript-world have been a long time ago. Am kinda happy with that, actually. :P
  • blue-sky
    blue-sky about 13 years
    Yes, thats the problem. The call seems to get cancelled because of other programs runnning, causing the shutdown to cancel. None of the above worked. Thanks for your comments.
  • notthetup
    notthetup about 13 years
    OK. Can you try to see if running this script before doing the shut down helps to go around issue with apps canceling shutdown.. stackoverflow.com/questions/495323/…

Related