OSX:如何检查 launchctl 命令的返回值

发布于 2024-09-30 00:07:11 字数 263 浏览 1 评论 0原文

在 OSX 上,使用 launchctl 启动和停止服务似乎很棒,但是我没有看到检查返回值以确保它确实启动。即使服务无法启动,返回值似乎总是返回为零

此 plist 文件包含错误,因此不会按预期启动 Drizzle

% launchctl start /Library/LaunchDaemons/org.drizzlebad.plist

% echo $?

0

除了检查此服务的 pid 文件是否存在之外,还有其他建议吗?

On OSX using launchctl to start and stop a service seems great however I don't see anyway to check the return value to make sure it really did start. Return value always seems to come back as zero even when the service failed to start

This plist file contains an error and thus will not start Drizzle as expected

% launchctl start /Library/LaunchDaemons/org.drizzlebad.plist

% echo $?

0

Any suggestions other than checking the existence of the pid file for this service?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柠檬 2024-10-07 00:07:11

您一定做错了什么,因为 launchctl 仅在成功时返回 0:

$ launchctl start foobar
launchctl start error: No such process
$ echo $?
1

此外,launchctl start 采用职位名称(通常是反向 DNS 样式的名称),而不是plist 的路径。 launchctl loadunload 采用 plist 路径。

如果您坚持认为它对您不起作用,您可以尝试仅在 launchctl list 的输出中查找您的作业名称,并在必要时解析字段。

You must be doing something wrong, because launchctl only returns 0 on success:

$ launchctl start foobar
launchctl start error: No such process
$ echo $?
1

Also, launchctl start takes the job title (generally a reverse-DNS-style name), not the path to a plist. It's launchctl load and unload that take a plist path.

If you insist that it doesn't work for you, you could try just grepping the output of launchctl list for your job name and, if necessary, parsing the fields.

睡美人的小仙女 2024-10-07 00:07:11

当您从终端执行 launchctl list 时,您会注意到 - 在第二列中...有一个主要由 0 和 - 组成的列表...如果您的程序错误退出,它将在第二列中显示退出代码...通常是 -1 或有时是 -251,或者无论什么情况...并且记住...只是因为您的 launchd 项目可能崩溃了... launchctl 可能仍然狂热地试图“让它工作” - 每10秒......所以不要让一个borked launchd进程永远徘徊,因为它肯定会影响性能..

用-w卸载它......然后plist 的完整路径,或者至少使用包标识符停止(并可选择删除它)。 -w 将在 /var/db/launchd 文件夹中的 plist 中创建一个秘密条目,告诉内核下次机器启动时不要加载它.. 无论“启用”的 XML 条目在 Plist 中可能会说什么。 ..所以请注意,关于下次启动时该项目是否复活,不仅仅有那个标志在起作用...

如果你愿意,你可以备份 tat var 文件夹深处的 overrides.plist 文件并重新开始,如果随着时间的推移,加载和卸载变得太复杂了……还有其他方法可以做到这一点,但我现在要闭嘴了……在我顺便指导您如何完全软管您的计算机之前……

when you do a launchctl list from the terminal, you'll notice - in the second column... there is a list of mostly 0's and -'s... if your program exited incorrectly, it will show the exit code in that second column.... usually a -1 or sometimes a -251, or whatever the case may be... and remember... just cause your launchd item might have crashed... launchctl may still feverishly be trying to "make it work" - every 10 seconds..... so don't let a borked launchd process linger forever, as it definitely impacts performance..

unload it with a -w.... and the full path to the plist, or at least stop (and optionally remove it) with the bundle identifier. -w will make a clandestine entry in a plist in your /var/db/launchd folder, telling the kernel not to load it next time the machine boots.. irrespective of what the "enabled" XML entry may say in the Plist.... so beware that there is more than just that flag at play as to whether the item is resurrected the next time you startup...

if you want you can back up that overrides.plist file deep in tat var folder and start over if it has gotten too convoluted with loads and unloads over time... there's other ways to do it, but i'm going to shut up now.. before I incidentally instruct you how to completely hose your computer...

多孤肩上扛 2024-10-07 00:07:11

为了后人。
当出现“输入/输出错误”时,我似乎在加载/卸载(而不是启动/停止)时得到零返回。

$ launchctl load foobar
Load failed: 5: Input/output error
Try running `launchctl bootstrap` as root for richer errors.
$ echo $?
0

这让我正在寻找错误

$ launchctl load foobar 2>&1 | grep 'Load failed'

For posterity.
I seem to get a return of zero on load/unload (not start/stop), when there is an "Input/output error".

$ launchctl load foobar
Load failed: 5: Input/output error
Try running `launchctl bootstrap` as root for richer errors.
$ echo $?
0

This leaves me grep'ing for the error

$ launchctl load foobar 2>&1 | grep 'Load failed'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文