且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何安全强制关闭Mac

更新时间:2023-01-12 20:05:49

shutdown命令向运行进程发送一个信号终止,做清理工作,如果需要。因此,一般来说,当应用程序接收到此信号( SIGTERM (inate))时,应该结束并退出。



IIRC在Snow Leopard(10.6)苹果添加了一个名为fast-shutdown(或类似)的东西,它会发送一个标记为正确的进程,用 SIGKILL 信号关闭它们下来没有机会进行清理工作。这应该使关闭更快。默认是应用程序仍然获得 SIGTERM ,并且必须选择加入 SIGKILL ;他们可以将自己标记为脏,即。 e。具有未保存的工作,不想被强行杀死。



因此,当在磁盘实用程序中间关闭时,将中止任何磁盘实用程序正在做,IMHO不会导致数据损坏一般。但是,根据当前正在运行的操作,可能会出现一个不完整的磁盘映像或半格式化的分区。



使用 cron $ c $

c>调度关机是一个可行的选项,如果你想它发生在指定的时间。如果您希望在登录后的一定时间后发生,您可以使用number参数关闭以指定从现在开始的8小时。

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?

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.