Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I'm looking for an auto shutdown app that will forcefully shut down the computer at an appointed time.

The most important part of this is that the shutdown should not be cancelable (unless the process is extremely onerous).

share|improve this question
    
What do you mean by "app"? Does it have to be a GUI program or is launchd (which I suggest below) acceptable? If you do what a GUI app, what do you want it to do? Prompt for the time that the computer should be shutdown, when run? –  TJ Luoma Sep 20 '11 at 18:05
add comment

7 Answers

up vote 9 down vote accepted
+50

Ok, so further to my previous answer, I can walk you through getting cron (a built in UNIX schedule service) to run a scheduled shutdown command for you.

It will run as root and will be forced.

  • Open Terminal (/Applications/Utilities/Terminal)
  • Input sudo crontab -e

You'll need to enter your login password and then press enter.

You'll now be in a text editor (vim). Carefully input the following keystrokes.

  • Press i once (this will allow you to enter text)

Let's say you want the machine to shutdown at 2am every day, we'd now enter:

* 2 * * * /sbin/shutdown -h now

If you wanted the machine to shutdown at 2:30am every day, you'd enter:

30 2 * * * /sbin/shutdown -h now
  • When you've typed this in, press esc
  • Then press shift+z shift+z (that's uppercase "z" twice, to writes the changes and quit the editor)

You should now be dropped back to the command line where you started.

You're done!

share|improve this answer
    
Don't you need to put sudo there? I know that will probably cause it to hang up, but I'm pretty sure you have to be root to run shutdown. –  daviesgeek Sep 17 '11 at 15:13
    
No exceptions needed, that's an amazing answer, quite happily surprised, especially after I replied to your rudely. Thank you very much for your help –  Harry Sep 17 '11 at 15:32
2  
@daveisgeek - You are totally right, but note that we startup crontab with sudo. This means that we are editing root's crontab and therefore any scheduled commands within will be executed as that user. –  macaco Sep 17 '11 at 15:59
    
Hey, I did this, and at the appointed time I got a dialog that says, this computer will shut down in 10 minutes? And it asks me to cancel. I don't know if that was the result of the cron thing, or the energy saver thing I also did. Is the cron thing supposed to force a shutdown without a cancel dialog? –  Harry Sep 18 '11 at 14:54
3  
shutdown is in /sbin which is probably not in the default $PATH of cron. To fix, do the sudo crontab -e thing again, type dd to delete the line (assuming the file looks like the one you linked above) and reenter the line as 30 22 * * * /sbin/shutdown -h now. –  patrix Sep 20 '11 at 21:07
show 4 more comments

This can be scheduled in System Preferences > Energy Saver > Schedule. I'm not certain that this will initiate a forced shutdown, you'd have to try it out. But that's a built in option to automate scheduled power down/up.

  • You can also forcibly shutdown the system with a terminal command (requires root):

    shutdown -h now

    You could put that command into a launchd or cron scheduled task. Check out this article for far more info on those two services.

  • You may also want to check out an application like the aptly named iWannaSleep (not sure if this forces shutdown).

share|improve this answer
    
What...??? I'm looking for an app. –  Harry Sep 17 '11 at 13:22
1  
You may want to appear less ungrateful, but if you reread my answer you'll see that I recommend an app and if you do a little independent research you'll see that there are actually a few more applications that have this ability! –  macaco Sep 17 '11 at 13:28
    
iWannaSleep is the first result that people recommend on google, and it doesn't do what I want. And you're recommending me learn some unix thing to shut down the computer? –  Harry Sep 17 '11 at 13:49
    
No I am grateful for your help, it's just that your help is completely befuddling. –  Harry Sep 17 '11 at 13:50
1  
I recommend it because it is an effective way to achieve what you want using built in tools. If you are unable to find an application that suits your needs then you may need to look to something along the lines of my suggestion. –  macaco Sep 17 '11 at 14:00
show 1 more comment

It can be canceled though:


Halt at yymmddhhmm:

shutdown -h 1109211555

Halt in 4 minutes:

shutdown -h +4

/Library/LaunchAgents/me.lri.forceshutdown.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>me.lri.forceshutdown</string>
    <key>ProgramArguments</key>
    <array>
        <string>shutdown</string>
        <string>-h</string>
        <string>now</string>

    </array>
    <key>StartCalendarInterval</key>
    <dict>
        <key>Hour</key>
        <integer>23</integer>
        <key>Minute</key>
        <integer>0</integer>
    </dict>
</dict>
</plist>

If the plist was owned by a normal user, trying to load it would result in the error launchctl: Dubious ownership on file (skipping):

sudo chown root /Library/LaunchAgents/me.lri.forceshutdown.plist

The agent can be loaded by logging out and back in, or with:

sudo launchctl load /Library/LaunchAgents/me.lri.forceshutdown.plist

sudo crontab -e

`08 16 * * * /sbin/shutdown -h now`

This would perform a normal non-forced shut down:

osascript -e 'tell app "System Events" to shut down'
share|improve this answer
    
Thanks for your help! +1 –  Harry Sep 21 '11 at 16:04
    
When adding the shutdown command to a launchd agent in /Library/LaunchAgents/, it didnt work and 'console' would log 'shutdown: NOT super-user'. Perhaps this would have worked in /System/Library/LaunchAgents (but you're not supposed to put stuff there :-)) I only needed a normal shutdown, so I went with the osascript, and put it in ~/Library/LaunchAgents/bla.shutdown.plist. Worked fine. –  commonpike Jan 28 '12 at 15:56
    
I had to use <code>osascript -e 'tell app "Finder" to shut down'</code> though –  commonpike Jan 28 '12 at 15:59
add comment

You can use shutdown directly to schedule a shutdown at any given time in the future:

shutdown -h time

where time specifies a future time in one of two formats: +number, or yymmddhhmm, where the year, month, and day may be defaulted to the current system values. The first form brings the system down in number minutes and the second at the absolute time specified.

share|improve this answer
add comment
cd ~/Downloads/

curl --remote-name http://dl.dropbox.com/u/18414/ase/com.tjluoma.forceshutdown.plist

# see note below

sudo mv com.tjluoma.forceshutdown.plist /Library/LaunchAgents

sudo chown root:wheel /Library/LaunchAgents/com.tjluoma.forceshutdown.plist

sudo launchctl load /Library/LaunchAgents/com.tjluoma.forceshutdown.plist

Note:

As written, the plist will cause the computer to be shutdown at 11pm (local time) every day.

If you want to change the time, edit the script in any text editor. Change the Hour and Minute keys, excerpted here:

<dict>
    <key>Hour</key>
    <integer>23</integer>
    <key>Minute</key>
    <integer>0</integer>
</dict>

(note the use of 24-hour time)

share|improve this answer
    
Thanks for your help! +1 –  Harry Sep 21 '11 at 16:04
add comment

Nicer Scheduled Shut Down

If you want to safely shut down your Mac at a scheduled time, consider Power Manager; it shuts down nicely without letting applications block the process:

  • All active users are given ample notification.
  • A large warning is shown shortly before shut down begins.
  • Running applications are asked nicely to quit.
  • Applications ignoring the nice request are more firmly quit.
  • Log out is left to complete, before the Mac is finally shut down.

It is very difficult for a rogue application or process to block these steps.

Why not shutdown

Using shutdown will force quit all processes on your Mac, including graphical applications. For many applications this is aggressive behaviour and does not provide the application with much opportunity to save state or data.

Schedule Assistant

Power Manager includes a Schedule Assistant task for shutting down to a schedule.

Power Manager - power off daily task

Disclosure: I work with the company who make Power Manager.

share|improve this answer
add comment

I'd take a look at this tech note by Apple

share|improve this answer
    
Thanks, what do I do now? –  Harry Sep 17 '11 at 14:10
    
you can use SendAppleEventToSystemProcess(kAEShutDown) then you can use the command killall -u username to kill all apps (since you don't want it cancelable) you might also want to check out the timer class: developer.apple.com/library/mac/#documentation/Cocoa/Reference/… –  Samantha Catania Sep 17 '11 at 14:28
1  
Great article, but OP was balking about using cron or launchd, I doubt he's looking to code his own solution to this problem. –  macaco Sep 17 '11 at 14:35
1  
Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. –  daviesgeek Nov 16 '11 at 18:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.