module Rinit::ProcessUtils

Public Instance Methods

get_pid_from_file(filename) click to toggle source

@private

# File lib/rinit/process_utils.rb, line 29
def get_pid_from_file(filename)
  begin
    pid = IO.readlines(filename)
  rescue Errno::ENOENT => e
    puts e.message + "---Are you sure it is running?"
    exit 1
  end
  # if the file is there but no pid was written to_i returns 0
  pid.empty? ? -1 : pid[0].to_i
end
is_process_running?(pidfile) click to toggle source

@private

# File lib/rinit/process_utils.rb, line 41
def is_process_running?(pidfile)
  pid = get_pid_from_file(pidfile)
  ProcTable.ps{ |process|
   return true if process.pid == pid
  }
  false
end
kill_process(pidfile) click to toggle source

@private

# File lib/rinit/process_utils.rb, line 60
def kill_process(pidfile)
  pid = get_pid_from_file(pidfile)
  # should never be negative unless there is an issue with writing the pid
  return if pid < 0
  begin
    Process.kill(9,pid)
  rescue Errno::ESRCH => e
    puts e.message
    puts "The pid file may not have been cleaned up properly."
    exit 1
  end
end
may_the_fork_be_with_you(command, pidfile) click to toggle source

@private

# File lib/rinit/process_utils.rb, line 9
def may_the_fork_be_with_you(command, pidfile)
  rd, wr = IO.pipe
  pid = fork do
    rd.close
    begin
      exec(command)
    rescue SystemCallError
      wr.write('!')
      exit 1
    end
  end
  wr.close
  command_result = if rd.eof?
                     write_pidfile(pid, pidfile)
                   else
                     nil
                   end
end
pidfile_cleanup(pidfile) click to toggle source

@private

# File lib/rinit/process_utils.rb, line 50
def pidfile_cleanup pidfile
  begin
    FileUtils.rm(pidfile)
  rescue Errno::ENOENT => e
    puts e.message
    exit 1
  end
end
write_pidfile(pid, pidfile) click to toggle source

@private

# File lib/rinit/process_utils.rb, line 74
def write_pidfile(pid, pidfile)
  File.open(pidfile, 'w') { |f| f.write(pid) }
end