只运行在关机脚本在Mac OS X(未注销或重新启动) [英] Run a script only at shutdown (not log off or restart) on Mac OS X
问题描述
有什么办法仅在关机时运行脚本?
Is there any way to run a script only at shutdown?
我的意思是,只有当计算机真的关停为关闭状态。这样做仅仅是注销或重新启动时,此脚本应该不能运行。
I mean, only when the computer is really shutting down to off state. This script should not run when doing just a log off or restart.
推荐答案
下面是不工作(我只是测试它)的方式,但它是没有经验的人相当的技术,而不是......我把一个包装器/ sbin目录/关闭。这将即使你关闭从GUI苹果菜单工作你的Mac。
Here is a way that does work (I just tested it) but it is quite technical and not for inexperienced people... I put a wrapper around /sbin/shutdown. This will work even if you shutdown your Mac from the Apple menu in the GUI.
基本上,你需要苏
来根,这样,和重命名现有的,苹果提供的关机
二进制 shutdown.orig
。
Basically, you need to su
to root, like this, and rename the existing, Apple-supplied shutdown
binary to shutdown.orig
.
su -
cd /sbin
mv shutdown shutdown.orig
然后创建一个名为关机
A bash脚本,你想要做什么,然后再高管
原Apple苹果提供的关机二进制文件。
Then you create a bash script called shutdown
that does what you want first, then execs
the original Apple-supplied shutdown binary.
#!/bin/bash
Do something you want done before shutdown
exec /sbin/shutdown.orig "$@"
有三件事情需要提防...
There are three things to watch out for...
1. Make all the permissions the same on shutdown as shutdown.orig
2. Parse the parameters to the originl shutdown and see if `-r` is one of them as this means it is a `restart` shutdown. You will also have to pass through the other parameters that Apple calls the script with - if any.
3. Apple may feel at liberty to overwrite your lovely, shiny, new `shutdown` script when updating OSX, so maybe abstract out the bulk of your personal shutdown script into another place so that you can easily re-insert a single-line call to it if/when Apple overwrites it at some point.
小心!并做好备份第一!
Be careful! And make a backup first!
这篇关于只运行在关机脚本在Mac OS X(未注销或重新启动)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!