Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A script to unload its associated job from launchd.
#!/usr/bin/env ruby
module LaunchdPlist
class << self
def find
plist = LaunchdPlist.new
plist.find
end
end
class LaunchdPlist
require 'pathname'
def boot_launchdaemons
Pathname.new("/Library/LaunchDaemons")
end
def user_launchdaemons
Pathname.new("~/Library/LaunchAgents").expand_path
end
def real_user
require 'etc'
Etc.getpwuid(Process.uid).name
end
def superuser?
real_user == "root"
end
def find
ld = nil
if superuser?
ld = boot_launchdaemons
(boot_launchdaemons+"label.plist").exist?
else
ld = user_launchdaemons
(user_launchdaemons+"label.plist").exist?
end
@plist = (ld+"#{@label}.plist")
self
end
def exist?
@plist.exist?
end
def to_s
@plist.to_s
end
def unload
if @plist.exist?
`launchctl unload #{@plist}`
else
raise "Couldnt find plist file '#{@plist}' to unload job label '#{@label}'"
end
end
def initialize *args, &blk
@label = ENV["LAUNCHD_JOB_LABEL"] || ARGV[0] || Pathname.new($0).basename(".rb").to_s
if @label.nil? || @label.empty?
raise "No launchd job label found. Please supply an environment variable $LAUNCHD_JOB_LABEL, or argv commandline argument"
end
end
end
end
plist = LaunchdPlist.find
# $stdin.close
# plist.unload
`launchctl unload #{plist}`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.