Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Safely Force Shutdown of Mac

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?

like image 581
John Gallagher Avatar asked Nov 15 '09 16:11

John Gallagher


2 Answers

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

like image 55
Peter Hosey Avatar answered Nov 16 '22 04:11

Peter Hosey


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.

like image 2
Daniel Schneller Avatar answered Nov 16 '22 03:11

Daniel Schneller