有没有办法只在关机时运行脚本?
我的意思是,只有当计算机真的关闭到关闭状态时才运行此脚本。 当仅注销或重新启动时,不应运行此脚本。
有没有办法只在关机时运行脚本?
我的意思是,只有当计算机真的关闭到关闭状态时才运行此脚本。 当仅注销或重新启动时,不应运行此脚本。
几天前我在Github上发布了一个可以在Mac OS X启动/关机时执行的配置/脚本。
基本上,在Mac OS X上,您可以/应该使用系统范围和每个用户的守护程序/代理配置文件
(plist)与bash脚本文件结合使用。这是一个您可以使用的plist文件示例:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key><string>boot.shutdown.script.name</string>
<key>ProgramArguments</key>
<array>
<string>SCRIPT_PATH/boot-shutdown.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>StandardOutPath</key>
<string>LOG_PATH/boot-shutdown.log</string>
<key>StandardErrorPath</key>
<string>LOG_PATH/boot-shutdown.err</string>
</dict>
</plist>
你可以将此文件放置在/Library/LaunchDaemons
中。有许多目录可以放置plist文件,这取决于你的需求、进程权限等因素。
你可以将此文件放入/Library/LaunchDaemons
中。有许多目录可供选择,具体取决于您的需求和该进程的权限等因素。
~/Library/LaunchAgents Per-user agents provided by the user.
/Library/LaunchAgents Per-user agents provided by the administrator.
/Library/LaunchDaemons System wide daemons provided by the administrator.
/System/Library/LaunchAgents Mac OS X Per-user agents.
/System/Library/LaunchDaemons Mac OS X System wide daemons.
这个脚本boot-shutdown.sh
会在每次启动/关机时被加载和执行。
#!/bin/bash
function shutdown()
{
# INSERT HERE THE COMMAND YOU WANT EXECUTE AT SHUTDOWN OR SERVICE UNLOAD
exit 0
}
function startup()
{
# INSERT HERE THE COMMAND YOU WANT EXECUTE AT STARTUP OR SERVICE LOAD
tail -f /dev/null &
wait $!
}
trap shutdown SIGTERM
trap shutdown SIGKILL
startup;
然后调用launchctl
命令来加载和卸载守护进程/代理。
sudo launchctl load -w /Library/LaunchDaemons/boot-shutdown-script.plist
"尽管OS X没有传统的BSD风格的关机挂钩,但I/O Kit在最近版本中提供了等效的功能。由于I/O Kit提供了这个功能,所以您必须从C++代码中调用它。"
"要注册通知,您需要调用registerSleepWakeInterest (在IOKit/RootDomain.h中描述)并注册休眠通知。如果系统即将关闭,您的处理程序将收到消息类型kIOMessageSystemWillPowerOff。如果系统即将重新启动,您的处理程序将获得消息类型kIOMessageSystemWillRestart。如果系统即将重新启动,则您的处理程序将获得消息类型kIOMessageSystemWillSleep。"
如您所见,重启消息与关机是不同的,因此您可以专门处理关机情况。
su
,并将现有的、由Apple提供的shutdown
二进制文件重命名为shutdown.orig
。su -
cd /sbin
mv shutdown shutdown.orig
接下来,您需要创建一个名为shutdown
的Bash脚本,先执行您想要的操作,然后再execs
原始的苹果提供的关机二进制文件。
#!/bin/bash
Do something you want done before shutdown
exec /sbin/shutdown.orig "$@"
有三个需要注意的地方...
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.
注意!首先要备份!
shutdown
在/sbin
中,该目录受rootless保护。 - Martijn Bleeker cat LogoutHook.sh
#!/bin/bash
export BLUEUTIL_ALLOW_ROOT=1
function shutdown()
{
echo `date` >> /Users/blanc/.LaunchShell/shutdown_date.log
echo `whoami` >> /Users/blanc/.LaunchShell/shutdown_date.log
/usr/local/bin/blueutil -p 0
sleep 1
#exit 0
}
shutdown;
sudo defaults write com.apple.loginwindow LogoutHook /Users/blanc/.LaunchShell/LogoutHook.sh
如果有人像我一样通过谷歌搜索类似但略有不同的问题来到这个答案。
由于我发现了CopyQ崩溃的错误https://github.com/hluk/CopyQ/issues/1301,我不得不编写一个cron脚本,以便在它崩溃时重新加载。 但是,如果在关闭或重启期间退出copyq,我不想重新加载copyq
最终我使用了
if ps aux | grep "Finder.app" | grep -v grep
then
echo "I am still running"
else
echo "I am in shutdown"
fi
为了检测系统是否正在关机,(您可以根据需要使用Chrome.app
或任何其他始终运行的应用程序)
虽然不是完美的解决方案,但它帮助我解决了类似的问题。