Class: Launchd::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/launchd/service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Service

NOTE: Only label and program_arguments are required. See ‘man launchd.plist` for all possible properties.



8
9
10
11
# File 'lib/launchd/service.rb', line 8

def initialize(opts = {})
  @options = opts
  @label = opts.fetch(:label)
end

Instance Attribute Details

#labelObject (readonly)

Returns the value of attribute label.



3
4
5
# File 'lib/launchd/service.rb', line 3

def label
  @label
end

#optionsObject (readonly) Also known as: to_hash

Returns the value of attribute options.



3
4
5
# File 'lib/launchd/service.rb', line 3

def options
  @options
end

Instance Method Details

#restartObject

restart (install & restart) the service.

If running as root, it will get installed as system-wide daemon, otherwise as agent.



18
19
20
21
22
# File 'lib/launchd/service.rb', line 18

def restart
  stop if running?
  save
  start
end

#running?Boolean

Returns true, when this service is runnung (loaded), otherwise false.

Returns:

  • (Boolean)


35
36
37
# File 'lib/launchd/service.rb', line 35

def running?
  !!system("launchctl list #{label}")
end

#startObject

Starts the service.



30
31
32
# File 'lib/launchd/service.rb', line 30

def start
  system("launchctl load -w #{label}")
end

#stopObject

Stops the service.



25
26
27
# File 'lib/launchd/service.rb', line 25

def stop
  system("launchctl unload -w #{label}")
end