nic*_*tme 7 launchd launchctl macos
我使用Apple 看似简单的文档创建了一个 LaunchDaemon 来运行我编写的 Node.js 脚本。
这是plist
文件。它基本上是 Apple 文档中的复制粘贴,设置为每 300 秒运行一次:
<?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>com.wintr.eodemail</string>
<key>ProgramArguments</key>
<array>
<string>~/projects/eod_email/eod</string>
</array>
<key>StartInterval</key>
<integer>300</integer>
<key>StandardOutPath</key>
<string>/var/log/eod-email.log</string>
<key>StandardErrorPath</key>
<string>/var/log/eod-email.log</string>
<key>Debug</key>
<true/>
</dict>
</plist>
Run Code Online (Sandbox Code Playgroud)
这是我遇到的错误/var/log/system.log
:
Jul 22 10:55:52 Nick-Cox com.apple.xpc.launchd[1] (com.wintr.eodemail[7097]): Service could not initialize: 14E46: xpcproxy + 13421 [1402][7D917364-B96E-3F93-B923-A89B5BF5736D]: 0x2
Run Code Online (Sandbox Code Playgroud)
我所做的:
-rw-r--r--
由 root 拥有)/Users/nickcox/projects/eod_email/eod
)的绝对路径并确保我运行launchctl unload (daemonname)
并launchctl load (daemon name)
根据那些 Apple 文档,这似乎比 cron 复杂得多,后者显然已被弃用。我需要做什么才能让这个脚本按计划运行?
开始使用 launchctl 绝对是一次令人沮丧的经历。我找到了很多解释你应该做什么的文章,但很少有可下载的样本。这是一个简单的 LaunchDaemon,它有望成为一个很好的起点。如果您不想复制和粘贴,可以在此处下载文件。
注意:您需要用您的用户名替换 MY_USER_NAME。plist 需要找到您的脚本。
// at ~/Desktop/testdaemon/com.wintr.eodemail.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>com.wintr.eodemail</string>
<key>Program</key>
<string>/Users/MY_USER_NAME/Desktop/testdaemon/testdaemon.sh</string>
<key>StandardErrorPath</key>
<string>/var/log/eod-email.log</string>
<key>StandardOutPath</key>
<string>/var/log/eod-email.log</string>
<key>RunAtLoad</key>
<true/>
<key>StartInterval</key>
<integer>15</integer>
</dict>
</plist>
Run Code Online (Sandbox Code Playgroud)
这是一个简单的守护程序脚本,它将日期时间附加到您桌面上的文件中。注意:由于脚本以 root 身份运行,波浪号 (~) 不会是您期望的主目录。
// at ~/Desktop/testdaemon/testdaemon.sh
#!/bin/sh
home="/Users/MYUSERNAME" ## note -- this will be run as root, ~ is not your normal user
now=$(date "+%Y-%m-%d %H.%M.%S")
echo $now >> "$home/Desktop/TestFile.txt"
Run Code Online (Sandbox Code Playgroud)
最后,我总是写一个小 shell 脚本来安装 LaunchDaemons,因为它很容易出错。由于 launchctl 以 root 身份运行您的脚本,因此它要求脚本的权限不能被其他人写入,因为这实际上会给他们 root 权限。
// ~/Desktop/testdaemon/install.sh
#!/bin/sh -e
plist_path="com.wintr.eodemail.plist"
plist_filename=$(basename "$plist_path")
install_path="/Library/LaunchDaemons/$plist_filename"
echo "installing launchctl plist: $plist_path --> $install_path"
sudo cp -f "$plist_path" "$install_path"
sudo chown root "$install_path"
sudo chmod 644 "$install_path"
sudo launchctl unload "$install_path"
sudo launchctl load "$install_path"
echo "to check if it's running, run this command: sudo launchctl list | grep wintr"
echo "to uninstall, run this command: sudo launchctl unload \"$install_path\""
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
15366 次 |
最近记录: |