如何调试未在启动时运行的Launchd脚本?

dd.*_*dd. 20 macos launchd

我有一些自制的启动脚本.但是,当我重新启动计算机时,我必须手动运行它们:

launchctl load -w ~/Library/LaunchAgents/com.mysql.mysqld.plist
Run Code Online (Sandbox Code Playgroud)

<?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>KeepAlive</key>
  <true/>
  <key>Label</key>
  <string>com.mysql.mysqld</string>
  <key>Program</key>
  <string>/Users/dash/.local/Cellar/mysql/5.1.49/bin/mysqld_safe</string>
  <key>RunAtLoad</key>
  <true/>
  <key>UserName</key>
  <string>dash</string>
  <key>WorkingDirectory</key>
  <string>/Users/dash/.local/var</string>
</dict>
</plist>
Run Code Online (Sandbox Code Playgroud)

我认为这应该在启动时发生.我错过了什么?

bgs*_*bgs 28

我发现你在plist中调试的最好方法:

<key>StandardErrorPath</key>
<string>/tmp/mycommand.err</string>
<key>StandardOutPath</key>
<string>/tmp/mycommand.out</string>
Run Code Online (Sandbox Code Playgroud)

打开控制台应用程序,在"所有消息"中,您应该在应用程序失败或成功时看到条目.像这样:

4/28/15 10:43:19.938 AM com.apple.xpc.launchd[1]: (mycommand[18704]) Service exited with abnormal code: 1
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是ProgramArguments将每个命令<string>项作为数组中的项.

编辑:在我的情况下,为shell脚本生成一个简单的包装器工作得更好.此脚本设置基本文件夹结构,以将shell脚本转换为OS X"app" - https://gist.github.com/mathiasbynens/674099.这可能对您的mysql -u arg1命令更有效.


sgo*_*n00 7

对我来说,到目前为止其他解决方案对我没有帮助。我的问题有点难以调试,因为 plist 文件是正确的,脚本在终端中单独运行良好。一切看起来都很好,但行不通。

我通过执行检查了日志文件

tail -f /var/log/system.log
Run Code Online (Sandbox Code Playgroud)

然后使用以下命令再次卸载并加载服务:

launchctl unload ~/Library/LaunchAgents/example.plist
launchctl load ~/Library/LaunchAgents/example.plist
Run Code Online (Sandbox Code Playgroud)

我从日志文件中发现了一条错误消息:

Program specified by service is not a Mach-O executable file.
Run Code Online (Sandbox Code Playgroud)

它到底意味着什么?我没有谷歌。但是,我觉得这是因为我没有#!/bin/bash在shell脚本的开头添加。因为我有时懒得加这一行。

添加标题后,一切正常。


Ala*_*ith 1

一种可能性:查看目录:

/private/var/db/launchd.db/
Run Code Online (Sandbox Code Playgroud)

并为您的用户修改“com.apple.launchd.peruser.###”文件。打开它,看看是否有类似以下的条目:

<key>com.mysql.mysqld.plist</key>
<dict>
    <key>Disabled</key>
    <true/>
</dict>
Run Code Online (Sandbox Code Playgroud)

如果是这样,请尝试将其设置为<false/>。查找相同内容的另一个文件是:

/private/var/db/launchd.db/com.apple.launchd/overrides.plist
Run Code Online (Sandbox Code Playgroud)