Created
August 19, 2010 13:39
-
-
Save dreamcat4/537897 to your computer and use it in GitHub Desktop.
A script to unload its associated job from launchd.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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