views:

972

answers:

6

What I want

I'm developing a little app to force me to only work at certain times of day - I need something to force me to stop working in the evenings so I can be more effective in the day.

The option within OS X to shut down my machine at a certain time is too easy to cancel. And you can always log back in afterwards.

I want my app to quit all applications whether they have unsaved work or not.

What I've tried

I thought of killing the loginwindow process, but I've read that this can cause data corruption.

I've come across the shutdown command - I'm using sudo shutdown -h +0 to shutdown immediately. This appears to be just the ticket, but I'm worried that it might cause data corruption if, say, Disk Utility is doing some kind of scan.

Is the shutdown command safe?

Can the shutdown command cause corruption? Or is it safe to use? Is there a better way of forcing shutdown safely?

A: 

Maybe cron is installed on your computer? It's wonderful =)

codebliss
cron has always been available as part of Mac OS X (now as part of launchd). That doesn't answer the question, though: cron will work for scheduling the shutdown, but it does not shut down the computer itself; he still needs to know *what* to schedule.
Peter Hosey
+1  A: 

The shutdown command sends running processes a signal to terminate, giving them a chance to do clean up work, if needed. So generally, when an application receives this signal (SIGTERM(inate)) it should wrap up and exit.

IIRC in Snow Leopard (10.6) Apple added something called fast-shutdown (or similar) which will send processes that have been flagged as being ok with it a SIGKILL signal, shutting them down without chance for cleanup work. This is supposed to make shutdown faster. The default is that applications still get SIGTERM and have to opt-in for SIGKILL; and they can mark themselves as "dirty", i. e. having unsaved work and do not want to be killed forcibly.

So while shutting down in the middle of a disk utility run will abort whatever disk utility is doing, IMHO it would not cause data corruption in general. However depending on the operation you are currently running, you could end up with an incomplete disk image or a half-formatted partition. Maybe you want to refrain from using it when you know the end of your configured work time is coming close.

Using cron to schedule the shutdown is a viable option if you want it to happen at a specified time. If you want it to happen after a certain amount of time after you log in, you could use the number parameter to shutdown to specify say 8 hours from now.

Daniel Schneller
Excellent answer, Daniel, thanks. It's interesting to hear more details on the fast shutdown in Snow Leopard I've heard about.
John Gallagher
+1  A: 

If you want to lose unsaved work then shutdown -h is your only answer.

However, anyone who has debugged a full-screen app on OS X knows that is it very easy (some say too easy) for an app to capture the screen and render the computer essentially useless (without SSHing from another computer to kill the process.) That's another alternative.

Darren
I don't necessarily want to lose unsaved work, but I'd put up a warning to myself 10 minutes before shutdown as a reminder. I had a similar system with another bit of software that's no longer available and I never lost any work in several months.Thanks for the extra info on full screen apps too, that's interesting and useful.
John Gallagher
+1  A: 

Use AppleScript to tell application "System Events" to shut down.

Peter Hosey
+1  A: 

Forcing your computer to shut down (and discard any unsaved work) doesn't sound like a good idea to me. Wouldn't it be easier and safer to just set an alarm clock to remind yourself when you should stop working, and walk away from your computer when it rings? (That's what I do.)

Edit: That might have come across as a bit rude, which was not my intention at all. (I had no intention of making fun of your question or anything like that.) I just think that this would be a better solution to this problem :)

Leif
@Leif - no worries, it didn't come across as rude at all. It's a perfectly valid comment. It would be both easier and safer to just set an alarm clock, but unfortunately I have zero willpower and I *know* I would start saying "Oh, just this one thing.." and the next time I looked at the clock it would be 2am. It just wouldn't work for me. I wish it did!
John Gallagher
A: 

the recommended way to schedule a shutdown of your computer on a regular basis is in the system preferences -> Energy Saver panel. Click on the "schedule" button in the lower right hand corner. the rest is self explanatory...

kent
From my question: "The option within OS X to shut down my machine at a certain time is too easy to cancel. And you can always log back in afterwards."
John Gallagher