如何安全地强制关闭 Mac
我想要什么
我正在开发一个小应用程序,迫使我只在一天中的特定时间工作 - 我需要一些东西来迫使我停止在晚上工作,这样我就可以在白天更有效率。
OS X 中在特定时间关闭计算机的选项太容易取消。之后您可以随时重新登录。
我希望我的应用程序退出所有应用程序,无论它们是否有未保存的工作。
我尝试过的
我想过终止登录窗口进程,但我读到这可能会导致数据损坏。
我遇到过 shutdown 命令 - 我正在使用 sudo shutdown -h +0 立即关闭。这似乎只是票证,但我担心如果磁盘实用程序正在进行某种扫描,它可能会导致数据损坏。
关闭命令安全吗?
关闭命令会导致损坏吗?或者使用安全吗?有没有更好的方法来强制安全关闭?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
对我来说,强制关闭计算机(并丢弃所有未保存的工作)听起来不是一个好主意。设置一个闹钟来提醒自己何时应该停止工作,并在铃声响起时离开计算机,不是更容易、更安全吗? (这就是我所做的。)
编辑:这可能会让人觉得有点粗鲁,这根本不是我的本意。 (我无意取笑你的问题或类似的问题。)我只是认为这将是解决这个问题的更好方法:)
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 :)
也许您的计算机上安装了 cron?太棒了 =)
Maybe cron is installed on your computer? It's wonderful =)
使用AppleScript
告诉应用程序“系统事件”关闭
。Use AppleScript to
tell application "System Events" to shut down
.shutdown 命令向正在运行的进程发送终止信号,让它们有机会在需要时进行清理工作。因此,通常,当应用程序收到此信号 (
SIGTERM
(inate)) 时,它应该结束并退出。Snow Leopard (10.6) 中的 IIRC Apple 添加了一种称为快速关闭(或类似)的功能,该功能将向已标记为正常运行的进程发送
SIGKILL
信号,将其关闭,而无需进行清理工作。这应该可以使关闭速度更快。默认情况下,应用程序仍然会收到SIGTERM
并且必须选择加入SIGKILL
;他们可以将自己标记为“肮脏”,即有未保存的工作并且不想被强行杀死。因此,虽然在磁盘实用程序运行过程中关闭会中止磁盘实用程序正在执行的任何操作,但恕我直言,它通常不会导致数据损坏。但是,根据您当前正在运行的操作,您最终可能会得到不完整的磁盘映像或半格式化的分区。当您知道配置的工作时间即将结束时,也许您不想使用它。
如果您希望在指定时间发生关闭,那么使用 cron 来安排关闭是一个可行的选择。如果您希望它在登录后一定时间后发生,您可以使用 number 参数来关闭以指定从现在起 8 小时后。
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 getSIGTERM
and have to opt-in forSIGKILL
; 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.如果您想要丢失未保存的工作,那么
shutdown -h
是您唯一的答案。然而,任何在 OS X 上调试过全屏应用程序的人都知道,应用程序实现 捕获屏幕 并使计算机基本无用(无需 SSH)从另一台计算机终止该进程。)这是另一种选择。
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.
定期关闭计算机的推荐方法是在系统首选项 ->节能面板。单击右下角的“计划”按钮。其余的都是不言自明的......
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...