Method: RunInBackground.start

Defined in:
lib/run_in_background.rb

.start(binary_path, binary_args, name, opts_specific = {}) ⇒ Object

Start a service/daemon.
It important to delete it after usage.

Arguments

  • binary_path - absolute path to the script that should be run in background

    NOTE for Linux script should be executable and with UNIX end-of-lines (LF).
    
  • binary_args - Array (not nil) of script’s command line arguments

  • name - service/daemon name.

    NOTE should be unique
    
  • opts_specific - Hash of platform specific options (only for more specific usage)

    For more information regarding such options see documentation for
    win32-sevice (Windows), daemons (Linux/Mac)
    

Example (LINUX)

RunInBackground.start "/home/user/test_app", [], "daemon_test", {:monitor => true}



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/run_in_background.rb', line 119

def RunInBackground.start binary_path, binary_args, name, opts_specific = {}
  Log.debug1("executable that should be run as daemon/service: #{binary_path}")
  Log.debug1("arguments: #{binary_args}")
  Log.debug1("specific options: #{opts_specific}")

  if binary_path == nil or binary_args == nil or name == nil
    Log.error("binary path, binary args, name arguments must be defined")
    raise ArgumentError.new("binary path, binary args, name arguments must be defined")
  end

  if OS == :WINDOWS
    new_binary_path = String.new(binary_path)
    new_binary_args = Array.new(binary_args)
    wrap_windows new_binary_path, new_binary_args
    start_windows new_binary_path, new_binary_args, name, opts_specific
  else  # OS == LINUX
    start_linux binary_path, binary_args, name, opts_specific
  end

  0.upto(TIMEOUT) do
    if exists?(name) && running?(name)
      puts "daemon/service #{name} started\n"
      Log.debug1("daemon/service #{name} started")
      return
    end
    sleep 1
  end
  # if got here then something gone wrong and daemon/service wasn't started in timely manner
  delete name if exists? name
  Log.error("daemon/service #{name} wasn't started in timely manner")
Log.flush
  raise "daemon/service #{name} wasn't started in timely manner"
end