Class: Trema::Process

Inherits:
Object
  • Object
show all
Defined in:
ruby/trema/process.rb

Overview

A class responsible for terminating processes.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pid_file, name) ⇒ Process

Returns a new instance of Process.



47
48
49
50
51
52
53
54
55
56
# File 'ruby/trema/process.rb', line 47

def initialize pid_file, name
  @name = name
  @pid_file = pid_file
  begin
    @pid = IO.read( @pid_file ).chomp.to_i
    @uid = File.stat( @pid_file ).uid
  rescue
    @pid_file = nil
  end
end

Class Method Details

.read(pid_file) ⇒ Process .read(pid_file, name) ⇒ Process

Returns the object that encapsulates the process details.

Overloads:

  • .read(pid_file) ⇒ Process

    Reads a process identification file and saves the process name process id and effective user id. The process name is inferred from the :pid_file.

    Parameters:

    • the full path name of the pid file.

  • .read(pid_file, name) ⇒ Process

    Reads a process identification file and saves the process name process id and effective user id.

    Parameters:

    • the full path name of the pid file.

    • the process name.

Returns:

  • the object that encapsulates the process details.



41
42
43
44
# File 'ruby/trema/process.rb', line 41

def self.read pid_file, name = nil
  name = File.basename( pid_file, ".pid" ) if name.nil?
  return new( pid_file, name )
end

Instance Method Details

#kill!void

This method returns an undefined value.

kills an active process.

Raises:

  • if failed to kill the process after a maximum number of attempts.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'ruby/trema/process.rb', line 67

def kill!
  return if @pid_file.nil?
  return if dead?
  puts "Shutting down #{ @name }..." if $verbose
  10.times do
    if @uid == 0
      sh "sudo kill #{ @pid } 2>/dev/null" rescue nil
    else
      sh "kill #{ @pid } 2>/dev/null" rescue nil
    end
    return
    # return if dead?
  end
  raise "Failed to shut down #{ @name }"
end