32

我有一些来自自制软件的 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>

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

4

6 回答 6

40

Best way I found to debug, in your 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.

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.

于 2015-04-28T17:49:14.160 回答
3

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

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

tail -f /var/log/system.log

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

launchctl unload ~/Library/LaunchAgents/example.plist
launchctl load ~/Library/LaunchAgents/example.plist

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

Program specified by service is not a Mach-O executable file.

它的真正含义是什么?我没有谷歌。但是,我觉得是因为我没有#!/bin/bash在shell脚本的开头添加。因为我有时懒得加这行。

添加标题后,一切正常。

于 2019-05-17T14:56:00.673 回答
1

一种可能性:查看目录:

/private/var/db/launchd.db/

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

<key>com.mysql.mysqld.plist</key>
<dict>
    <key>Disabled</key>
    <true/>
</dict>

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

/private/var/db/launchd.db/com.apple.launchd/overrides.plist
于 2011-07-31T03:42:11.383 回答
1

启动命令需要作业标签作为它的参数,因此您可以使用以下命令启动它...

launchctl start com.myfile.hostname.plist

要停止,只需执行以下操作...

launchctl stop com.myfile.hostname.plist

完成所有测试后,您将注销然后登录以加载它,或者如果您的 plist 文件位于用户库文件夹中,请键入以下...

launchctl load ~/Library/LaunchAgents/com.myfile.hostname.plist
于 2015-07-06T14:12:34.320 回答
0

尝试重命名它。将文件名更改为:

~/Library/LaunchAgents/com.mysql.mysqld2.plist

和 plist 中的标签部分:

<key>Label</key>
<string>com.mysql.mysqld2</string>

如果您保存备份副本,请确保将其移出 ~/Library/LaunchAgents/ 目录。

最后,不要使用launchctl 来加载它,只需注销并重新登录。这将让launchd 自行从您的“LaunchAgents”目录中获取它,并从混合中再取出一个变量(即launchctl)。

于 2011-07-31T12:52:51.057 回答
0

从 OSX Yosemite 10.10 起launchctl,命令已更改。

需要以下命令才能在reboot.

sudo launchctl bootstrap system /Library/LaunchDaemons/${YOUR_SERVICE_NAME}.plist
sudo launchctl enable system/${YOUR_SERVICE_NAME}
sudo launchctl kickstart -kp system/${YOUR_SERVICE_NAME}

注意:它将使用 root 用户启动服务并在系统范围内启动。

参考:Launchctl 手册页(https://ss64.com/osx/launchctl.html

于 2021-11-25T05:37:55.513 回答