Convert Launchd KeepAlive Configuration to JSON
Convert the KeepAlive dictionary in launchd plist files to JSON. Understand the various KeepAlive sub-keys and their boolean/dictionary forms.
Detailed Explanation
Launchd KeepAlive: Simple and Complex Forms
The KeepAlive key in a launchd plist can be either a simple boolean or a dictionary with conditional restart rules. This dual nature makes it an interesting conversion case.
Simple Boolean Form
<key>KeepAlive</key>
<true/>
This becomes "KeepAlive": true in JSON -- the service restarts unconditionally whenever it exits.
Dictionary Form
<key>KeepAlive</key>
<dict>
<key>SuccessfulExit</key>
<false/>
<key>NetworkState</key>
<true/>
<key>PathState</key>
<dict>
<key>/tmp/run-service</key>
<true/>
</dict>
</dict>
JSON Output (Dictionary Form)
{
"KeepAlive": {
"SuccessfulExit": false,
"NetworkState": true,
"PathState": {
"/tmp/run-service": true
}
}
}
Conditional Restart Rules
| Sub-Key | Meaning |
|---|---|
SuccessfulExit |
Restart only on non-zero exit (when false) |
NetworkState |
Restart when network is available (when true) |
PathState |
Restart based on file existence |
Crashed |
Restart only after crash (when true) |
Type Flexibility in Plist
This example shows how the same plist key (KeepAlive) can be either a boolean or a dictionary depending on configuration needs. The JSON representation preserves this flexibility -- a consuming application can check typeof keepAlive to determine which form is being used.
Use Case
Valuable for system administrators writing fleet management scripts that need to parse and modify launchd KeepAlive rules programmatically using JSON processing tools.