OS X launchd 脚本无法 运行 启动?

OS X launchd script fails to run at startup?

我在启动时为 运行 通用脚本创建了一个 launchd 脚本,但我无法让它工作。我是 运行ning OS X Yosemite 10.10.5。这是我的 /Library/LaunchDaemons/com.craig.startup.items 文件(我只是注意到它没有“.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.craig.startup.items</string>
    <key>ProgramArguments</key>
    <array>
        <string>/bin/sh</string>
        <string>/Users/cliff/startup/onlaunchd</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <false/>
</dict>
</plist>

并且 /Users/cliff/startup/onlaunchd 包含以下内容:

#!/bin/sh

log=/Users/cliff/startup/status.log
echo "`date` running startup scripts." >> $log
pushd /Users/cliff/startup/scripts/
for each in `ls`; do echo "`date` Running $each" >> $log; sh $each; done
popd

还涉及更多脚本,但 onlaunchd 从未触发,因为我从未在 /Users/cliff/startup/status.log 中看到日志输出。如果我 运行 它直接作为 root 就可以正常工作。除了可能的扩展之外,我还缺少什么?

重启前你应该运行守护进程:

sudo launchctl load -w /Library/LaunchDaemons/com.craig.startup.items.plist

另外请设置 +x 权限 chmod +x /Users/cliff/startup/onlaunchd 并更新 plist 代码:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
   <key>Label</key>
   <string>com.user.loginscript</string>
   <key>Program</key>
   <string>/Users/cliff/startup/onlaunchd</string>
   <key>RunAtLoad</key>
   <true/>
</dict>
</plist>