Skip to content

Instantly share code, notes, and snippets.

@btm
Last active June 4, 2020 23:48
Show Gist options
  • Save btm/193e27d2e6abd3e77bfbd840e757d3d6 to your computer and use it in GitHub Desktop.
Save btm/193e27d2e6abd3e77bfbd840e757d3d6 to your computer and use it in GitHub Desktop.
parsing 'launchctl print' even though apple says not to because we are wild and crazy kids
require "pp"
WORD_REGEX = '([\w.:\/\(\)" -]+)' # basically match everything but [{}=>]
launchctl_output = `launchctl print system/com.opscode.chef-client`
hash = { }
hash_index = [ ]
launchctl_output.each_line do |line|
case line
when /#{WORD_REGEX} = {/ # start of group
key = $1
hash_index.push(hash)
hash = hash[key] = { }
when /^\s*}\s*$/ # end of group
hash = hash_index.pop
when /^\s*#{WORD_REGEX} =>? #{WORD_REGEX}/ # key/value pair
# most lines are 'key = value' but a couple like environment are 'key => value'
# doesn't capture the line 'submitted job. ignore execute allowed' but thats ok
hash.store($1, $2)
end
end
pp hash
{"com.opscode.chef-client"=>
{"active count"=>"1",
"copy count"=>"0",
"one shot"=>"0",
"path"=>"/Library/LaunchDaemons/com.chef.chef-client.plist",
"state"=>"running",
"program"=>"/usr/local/bin/chef-client",
"arguments"=>{},
"default environment"=>{"PATH"=>"/usr/bin:/bin:/usr/sbin:/sbin"},
"environment"=>{"XPC_SERVICE_NAME"=>"com.opscode.chef-client"},
"domain"=>"com.apple.xpc.launchd.domain.system",
"minimum runtime"=>"10",
"exit timeout"=>"5",
"runs"=>"10",
"successive crashes"=>"0",
"pid"=>"30289",
"immediate reason"=>"interval",
"forks"=>"55",
"execs"=>"1",
"initialized"=>"1",
"trampolined"=>"1",
"started suspended"=>"0",
"proxy started suspended"=>"0",
"last exit code"=>"1",
"event triggers"=>{},
"endpoints"=>{},
"dynamic endpoints"=>{},
"pid-local endpoints"=>{},
"instance-specific endpoints"=>{},
"event channels"=>
{"\"com.apple.launchd.helper\""=>
{"port"=>"0x45413",
"active"=>"0",
"managed"=>"1",
"reset"=>"0",
"hide"=>"0"}},
"sockets"=>{},
"instances"=>{},
"spawn type"=>"daemon",
"spawn role"=>"(null)",
"jetsam priority"=>"3",
"jetsam memory limit (active)"=>"(unlimited)",
"jetsam memory limit (inactive)"=>"(unlimited)",
"jetsamproperties category"=>"daemon",
"jetsam thread limit"=>"32",
"cpumon"=>"default",
"run interval"=>"1800 seconds",
"properties"=>
{"partial import"=>"0",
"launchd bundle"=>"0",
"xpc bundle"=>"0",
"keepalive"=>"0",
"runatload"=>"1",
"low priority i/o"=>"0",
"low priority background i/o"=>"0",
"dataless file mode"=>"0",
"legacy timer behavior"=>"0",
"exception handler"=>"0",
"multiple instances"=>"0",
"supports transactions"=>"0",
"supports pressured exit"=>"0",
"supports idle hysteresis"=>"0",
"enter kdp before kill"=>"0",
"wait for debugger"=>"0",
"app"=>"0",
"system app"=>"0",
"creates session"=>"0",
"inetd-compatible"=>"0",
"inetd listener"=>"0",
"abandon process group"=>"0",
"one-shot"=>"0",
"event monitor"=>"0",
"penalty box"=>"0",
"pended non-demand spawn"=>"0",
"role account"=>"0",
"launch only once"=>"0",
"system support"=>"0",
"app-like"=>"0",
"inferred program"=>"0",
"joins gui session"=>"0",
"joins host session"=>"0",
"parameterized sandbox"=>"1",
"resolve program"=>"0",
"abandon coalition"=>"0",
"high bits aslr"=>"0",
"extension"=>"0",
"nano allocator"=>"0",
"no initgroups"=>"0",
"start on fs mount"=>"0",
"endpoints initialized"=>"1",
"is copy"=>"0",
"disallow all lookups"=>"0",
"system service"=>"0",
"protected by submitter"=>"0"}}}
com.opscode.chef-client = {
active count = 1
copy count = 0
one shot = 0
path = /Library/LaunchDaemons/com.chef.chef-client.plist
state = running
program = /usr/local/bin/chef-client
arguments = {
}
default environment = {
PATH => /usr/bin:/bin:/usr/sbin:/sbin
}
environment = {
XPC_SERVICE_NAME => com.opscode.chef-client
}
domain = com.apple.xpc.launchd.domain.system
minimum runtime = 10
exit timeout = 5
runs = 11
successive crashes = 0
pid = 30460
immediate reason = one-shot
forks = 1
execs = 1
initialized = 1
trampolined = 1
started suspended = 0
proxy started suspended = 0
last exit code = 1
event triggers = {
}
endpoints = {
}
dynamic endpoints = {
}
pid-local endpoints = {
}
instance-specific endpoints = {
}
event channels = {
"com.apple.launchd.helper" = {
port = 0x45413
active = 0
managed = 1
reset = 0
hide = 0
}
}
sockets = {
}
instances = {
}
spawn type = daemon
spawn role = (null)
jetsam priority = 3
jetsam memory limit (active) = (unlimited)
jetsam memory limit (inactive) = (unlimited)
jetsamproperties category = daemon
submitted job. ignore execute allowed
jetsam thread limit = 32
cpumon = default
run interval = 1800 seconds
properties = {
partial import = 0
launchd bundle = 0
xpc bundle = 0
keepalive = 0
runatload = 1
low priority i/o = 0
low priority background i/o = 0
dataless file mode = 0
legacy timer behavior = 0
exception handler = 0
multiple instances = 0
supports transactions = 0
supports pressured exit = 0
supports idle hysteresis = 0
enter kdp before kill = 0
wait for debugger = 0
app = 0
system app = 0
creates session = 0
inetd-compatible = 0
inetd listener = 0
abandon process group = 0
one-shot = 0
event monitor = 0
penalty box = 0
pended non-demand spawn = 0
role account = 0
launch only once = 0
system support = 0
app-like = 0
inferred program = 0
joins gui session = 0
joins host session = 0
parameterized sandbox = 1
resolve program = 0
abandon coalition = 0
high bits aslr = 0
extension = 0
nano allocator = 0
no initgroups = 0
start on fs mount = 0
endpoints initialized = 1
is copy = 0
disallow all lookups = 0
system service = 0
protected by submitter = 0
}
}
com.opscode.chef-client = {
active count = 0
copy count = 0
one shot = 0
path = /Library/LaunchDaemons/com.chef.chef-client.plist
state = waiting
program = /usr/local/bin/chef-client
arguments = {
}
default environment = {
PATH => /usr/bin:/bin:/usr/sbin:/sbin
}
environment = {
XPC_SERVICE_NAME => com.opscode.chef-client
}
domain = com.apple.xpc.launchd.domain.system
minimum runtime = 10
exit timeout = 5
runs = 11
successive crashes = 0
last exit code = 1
event triggers = {
}
endpoints = {
}
dynamic endpoints = {
}
pid-local endpoints = {
}
instance-specific endpoints = {
}
event channels = {
"com.apple.launchd.helper" = {
port = 0x45413
active = 0
managed = 1
reset = 0
hide = 0
}
}
sockets = {
}
instances = {
}
spawn type = daemon
spawn role = (null)
jetsam priority = 3
jetsam memory limit (active) = (unlimited)
jetsam memory limit (inactive) = (unlimited)
jetsamproperties category = daemon
submitted job. ignore execute allowed
jetsam thread limit = 32
cpumon = default
run interval = 1800 seconds
properties = {
partial import = 0
launchd bundle = 0
xpc bundle = 0
keepalive = 0
runatload = 1
low priority i/o = 0
low priority background i/o = 0
dataless file mode = 0
legacy timer behavior = 0
exception handler = 0
multiple instances = 0
supports transactions = 0
supports pressured exit = 0
supports idle hysteresis = 0
enter kdp before kill = 0
wait for debugger = 0
app = 0
system app = 0
creates session = 0
inetd-compatible = 0
inetd listener = 0
abandon process group = 0
one-shot = 0
event monitor = 0
penalty box = 0
pended non-demand spawn = 0
role account = 0
launch only once = 0
system support = 0
app-like = 0
inferred program = 0
joins gui session = 0
joins host session = 0
parameterized sandbox = 1
resolve program = 0
abandon coalition = 0
high bits aslr = 0
extension = 0
nano allocator = 0
no initgroups = 0
start on fs mount = 0
endpoints initialized = 1
is copy = 0
disallow all lookups = 0
system service = 0
protected by submitter = 0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment