OS X-真正的“按需"特权帮助器工具 [英] OS X - Truly 'On Demand' Privileged Helper Tool

查看:133
本文介绍了OS X-真正的“按需"特权帮助器工具的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序需要将文件写入文件系统上的受限位置.
为此,我使用以root用户身份运行的帮助程序工具.

My application needs to write files to restricted places on the filesystem.
To do so, I use a helper tool that is run as root.

一切正常,遵循

Everything is working correctly, following this example, my helper tool is correctly installed and run. I use this solution to be able to "wake" the helper tool, otherwise it isn't launched more than one time after initial load.

帮助程序工具正在等待来自主应用程序的消息,并正确执行它们.我也有一种要求工具关闭的消息,该消息也可以正常工作.

The helper tool is waiting for messages to come from the main application, and executes them correctly. I also have a kind of message that ask the tool to shutdown, which also works correctly.

问题在于,无论我在launchd.plist文件中指定了什么键,launchd每次退出时都会重新启动该工具.
我不希望帮助程序工具永远运行,等待消息,而只能按需启动.

The problem is that launchd relaunch the tool every time it quits, no matter what keys I specify in the launchd.plist file.
I don't want the helper tool to run forever, waiting for messages, but instead start only on demand.

问题:每次结束时重新启动助手工具是否正常?如果不是,如何让它死掉并按需重启?

Question : is it normal that the helper tool is restarted every time it ends ? If not, how to let it die and restart on demand ?

这是launchd.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>
        <dict>
            <key>SuccessfulExit</key>
            <false/>
        </dict>
        <key>RunAtLoad</key>
        <true/>
        <key>Label</key>
        <string>com.mycompany.Helper</string>
        <key>MachServices</key>
            <dict>
                <key>com.mycompany.Helper</key>
                <true/>
            </dict>
     </dict>
</plist>

当然,我尝试了键和值的各种组合,以及辅助工具的各种退出状态代码. launchd管理流程没有任何改变.

Of course, I tried various combinations of keys and values, and also various exit status codes for the helper tool. It didn't changed in any way launchd manages the process.

推荐答案

在终端中键入man launchd.plist可提供launchd可以理解的所有选项.您可以找到的那些(重点是我的):

Typing man launchd.plist into Terminal gives you all the options that launchd understands. Among those you can find (emphasis mine):

KeepAlive<布尔值或事物词典>

可选 键用于控制您的作业是保持连续运行还是让需求和条件控制调用. 默认值为false ,因此只有需求才能开始工作.可以将值 设置为true ,以无条件地保持作业的正常运行.可选地,可以指定条件字典以选择性地控制启动是否使作业保持活动状态.如果提供了多个密钥,则对它们启动或"运算,从而为作业提供最大的灵活性,以完善逻辑并在必要时停止.如果launchd发现没有理由重新启动作业,则它将基于需求进行回退.配置为保持活动状态时快速且频繁退出的作业将受到限制,以节省系统资源.

This optional key is used to control whether your job is to be kept continuously running or to let demand and conditions control the invocation. The default is false and therefore only demand will start the job. The value may be set to true to unconditionally keep the job alive. Alternatively, a dictionary of conditions may be specified to selectively control whether launchd keeps a job alive or not. If multiple keys are provided, launchd ORs them, thus providing maximum flexibility to the job to refine the logic and stall if necessary. If launchd finds no reason to restart the job, it falls back on demand based invocation. Jobs that exit quickly and frequently when configured to be kept alive will be throttled to conserve system resources.

您不会写出键和值的各种组合"完全是.但是从本文档中,如果您希望按需启动守护程序,则应简单地从plist中删除<key>KeepAlive</key>或将其值设置为<false/>而不是<dict>...</dict>.

You don't write what your "various combinations of keys and values" were exactly. But from this documentation, if you want a daemon that gets launched on demand, you should simply drop the <key>KeepAlive</key> from your plist or have its value set to <false/> instead of a <dict>...</dict>.

这篇关于OS X-真正的“按需"特权帮助器工具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆