Module: Rinit::ProcessUtils

Includes:
Sys
Included in:
Rinit
Defined in:
lib/rinit/process_utils.rb

Instance Method Summary collapse

Instance Method Details

#get_pid_from_file(filename) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# 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) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
# 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) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# 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) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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) ⇒ Object



50
51
52
53
54
55
56
57
# 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) ⇒ Object



74
75
76
# File 'lib/rinit/process_utils.rb', line 74

def write_pidfile(pid, pidfile)
  File.open(pidfile, 'w') { |f| f.write(pid) }
end