【问题标题】:How does StartInterval key affects the started daemonStartInterval 键如何影响已启动的守护进程
【发布时间】:2014-10-29 09:55:01
【问题描述】:

我有一个作为守护进程运行的应用程序。我在 iOS 8 设备上将应用 plist 放在 /Library/LaunchDaemons 下,并通过执行命令启动它

launchctl load /Library/LaunchDaemons/com.mycompany.testapp.plist

在我的 laumchd plist 中注意,应用程序通过执行命令作为守护进程运行

我想让这个应用程序仅在它崩溃或被杀死时重新启动。如果我故意使用代码 0 退出它,我不希望它重新启动。我试过下面的配置。这适用于 iOS 7,但不适用于 iOS 8。

<key>KeepAlive</key>
<dict>
    <key>SuccessfulExit</key>
    <true/>
</dict>

因此我尝试添加另一个设置为 10 的键 StartInterval

<key>StartInterval</key>
<integer>10</integer>

我通过使用代码 0 退出并使用命令 kill -9 [PID] 终止我的应用程序来测试此场景。此键使我的应用程序在被杀死 10 秒后重新启动。但是,当我的应用程序运行时,我担心这个键的结果。

这个键对启动和运行的应用程序有影响吗?我已经监视了日志,似乎 StartInterval 键对正在运行的守护程序没有任何作用。但是,我不太确定。你能解释更多关于它的信息吗?非常感谢。

【问题讨论】:

    标签: ios iphone macos plist launchd


    【解决方案1】:

    根据此链接http://pitaya.ch/documentation/iOS8_0APIDiffs.html,标题中所有到launchd 的痕迹都已消失。 (搜索/usr/include/launch.h,您会看到它已被删除) 似乎是因为移动到新的libxpc 作为launchd 的基础(参见http://technologeeks.com/docs/launchd.pdf 中的最后一张幻灯片)。

    似乎他们在 iOS 8 中改变了launchd 的行为。Apple 自己的 LaunchDaemons(位于/System/Library/LaunchDaemons/ 下)中的一些也有一个字典格式的KeepAlive 属性。如果你杀死它们,它们会立即被launchd 重生。

    但是经过一些测试后,让KeepAlive-property 工作的唯一方法是添加添加LaunchEvents(参见http://technologeeks.com/docs/launchd.pdf 中的幻灯片21ff)和/或MachServices 到plist 或更改@ 987654334@-属性到:

    <key>KeepAlive</key>
    <true/>
    

    但是除了执行launchctl unload /Library/LaunchDaemons/com.mycompany.testapp.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>Program</key>
        <string>/path/to/your/daemon</string>
        <key>RunAtLoad</key>
        <true/>
        <key>Label</key>
        <string>com.mycompany.testapp</string>
        <key>EnablePressuredExit</key>
        <false/>
        <key>UserName</key>
        <string>root</string>
        <key>JetsamProperties</key>
        <dict>
            <key>JetsamPriority</key>
            <integer>-49</integer>
            <key>JetsamMemoryLimit</key>
            <integer>10000</integer>
        </dict>
        <key>POSIXSpawnType</key>
        <string>Adaptive</string>
        <key>Disabled</key>
        <false/>
        <key>ThrottleInterval</key>
        <integer>5</integer>
        <key>KeepAlive</key>
        <dict>
            <key>SuccessfulExit</key>
            <false/>
            <key>Crashed</key>
            <true/>
        </dict>
        <key>MachServices</key>
        <dict>
            <key> com.mycompany.testapp.someMachService</key>
            <true/>
        </dict>
        <key>EnableTransactions</key>
        <false/>
        <key>LaunchEvents</key>
        <dict>
            <key>com.apple.notifyd.matching</key>
            <dict>
                <key>SignificantTimeChangeNotification</key>
                <dict>
                    <key>Notification</key>
                    <string>SignificantTimeChangeNotification</string>
                </dict>
                <key>SomeMoreNotifications</key>
                [...]
            </dict>
        </dict>
    </dict>
    </plist>
    

    关于StartInterval-property:除了KeepAlive-property 之外,对于一些重要的守护进程(例如com.apple.locationd),即使Apple 也有这个属性,而且它们看起来运行得很好。所以我认为你不必担心......

    【讨论】:

      【解决方案2】:

      将 false 与 SuccessfulExit 键一起使用。

      SuccessfulExit 如果为真,只要程序退出并且退出状态为零,作业就会重新启动。 如果为 false,则作业将以相反的条件重新启动。这个键意味着“RunAtLoad” 设置为 true,因为作业至少需要运行一次才能获得退出状态。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-10-25
        • 1970-01-01
        • 1970-01-01
        • 2016-01-14
        • 1970-01-01
        • 1970-01-01
        • 2012-01-13
        相关资源
        最近更新 更多