macos 如何调试不在启动时运行的 Launchd 脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6337513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I debug a Launchd script that doesn't run on startup?
提问by dd.
I have some Launchd scripts from homebrew. However I have to manually run them when I restart my computer:
我有一些来自自制软件的 Launchd 脚本。但是,当我重新启动计算机时,我必须手动运行它们:
launchctl load -w ~/Library/LaunchAgents/com.mysql.mysqld.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>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>
I thought this should happen on startup. What am I missing?
我认为这应该在启动时发生。我错过了什么?
回答by bgs
Best way I found to debug, in your plist:
我发现调试的最佳方式,在你的 plist 中:
<key>StandardErrorPath</key>
<string>/tmp/mycommand.err</string>
<key>StandardOutPath</key>
<string>/tmp/mycommand.out</string>
Open Console app, in "All Messages" you should see entries when your app fails or succeeds. Like this:
打开控制台应用程序,在“所有消息”中,当您的应用程序失败或成功时,您应该会看到条目。像这样:
4/28/15 10:43:19.938 AM com.apple.xpc.launchd[1]: (mycommand[18704]) Service exited with abnormal code: 1
The issue I had was with ProgramArguments takes each item of command as <string>
item in the array.
我遇到的问题是 ProgramArguments 将命令的每个项目作为<string>
数组中的项目。
EDIT:
In my case, generating a simple wrapper for shell script worked even better.
This script sets up basic folder structure to make a shell script into an OS X "app" - https://gist.github.com/mathiasbynens/674099. This might work better for your mysql -u arg1
command.
编辑:就我而言,为 shell 脚本生成一个简单的包装器效果更好。此脚本设置基本文件夹结构,将 shell 脚本制作成 OS X“应用程序” - https://gist.github.com/mathiasbynens/674099。这可能更适合您的mysql -u arg1
命令。
回答by Alan W. Smith
One possibility: Look in the directory:
一种可能:查看目录:
/private/var/db/launchd.db/
and fine the "com.apple.launchd.peruser.###" file for your user. Open that and see if there is a entry like:
并为您的用户创建“com.apple.launchd.peruser.###”文件。打开它,看看是否有这样的条目:
<key>com.mysql.mysqld.plist</key>
<dict>
<key>Disabled</key>
<true/>
</dict>
If so, try setting it to <false/>
. Another file to look in for the same thing is:
如果是这样,请尝试将其设置为<false/>
。另一个要查找相同内容的文件是:
/private/var/db/launchd.db/com.apple.launchd/overrides.plist
回答by Adam
The start command expects the job Labelas it's argument, so you would start it using the following...
start 命令需要将作业标签作为参数,因此您可以使用以下命令启动它...
launchctl start com.myfile.hostname.plist
To stop, simply do the following...
要停止,只需执行以下操作...
launchctl stop com.myfile.hostname.plist
Once all your testing is complete you would log out then in to load it or if your plist file is in the users Library folder type the following...
完成所有测试后,您将注销然后登录以加载它,或者如果您的 plist 文件位于用户库文件夹中,请键入以下内容...
launchctl load ~/Library/LaunchAgents/com.myfile.hostname.plist
回答by sgon00
For me, other solutions so far do not help me. My problem is kinda hard to debug, because the plist file is correct, the script is running fine alone in a terminal. Everything looks fine, but doesn't work.
对我来说,到目前为止的其他解决方案对我没有帮助。我的问题有点难以调试,因为 plist 文件是正确的,脚本在终端中单独运行良好。一切看起来都很好,但不起作用。
I checked the log file by executing
我通过执行检查了日志文件
tail -f /var/log/system.log
And then by unloading and loading the service again with the commands:
然后通过以下命令卸载和再次加载服务:
launchctl unload ~/Library/LaunchAgents/example.plist
launchctl load ~/Library/LaunchAgents/example.plist
I found an error message from the log file:
我从日志文件中发现了一条错误消息:
Program specified by service is not a Mach-O executable file.
What does it really mean? I didn't google. But, I feel it's because I didn't add #!/bin/bash
at the beginning of the shell script. Because I am lazy to add this line sometimes.
它的真正含义是什么?我没有谷歌。但是,我觉得是因为我没有#!/bin/bash
在shell脚本的开头添加。因为有时候懒得加这行。
After adding the heading, everything works fine.
添加标题后,一切正常。
回答by Alan W. Smith
Try renaming it. Change the filename to:
尝试重命名它。将文件名更改为:
~/Library/LaunchAgents/com.mysql.mysqld2.plist
and the Label section in the plist to:
和 plist 中的标签部分:
<key>Label</key>
<string>com.mysql.mysqld2</string>
If you save a backup copy, make sure to move it outside of your ~/Library/LaunchAgents/ directory.
如果您保存备份副本,请确保将其移动到您的 ~/Library/LaunchAgents/ 目录之外。
Finally, instead of using launchctl to load it, just logout and log back in. That will let launchd pick it up from your "LaunchAgents" directory by itself and take one more variable (i.e. launchctl) out of the mix.
最后,不是使用launchctl 加载它,只需注销并重新登录。这将使launchd 自己从“LaunchAgents”目录中提取它,并从组合中取出另一个变量(即launchctl)。