Class: Lsof

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

Class Method Summary collapse

Class Method Details

.find_pids_cmd(port) ⇒ Object



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

def find_pids_cmd(port)
  "lsof -i tcp:#{port} | grep '(LISTEN)' | awk '{print $2}'"
end

.kill(port) ⇒ Object



3
4
5
6
# File 'lib/lsof.rb', line 3

def kill(port)
  pid = listener_pids(port)
  `#{find_pids_cmd(port)} | xargs kill 2>&1`
end

.listener_pids(port) ⇒ Object



18
19
20
21
22
23
# File 'lib/lsof.rb', line 18

def listener_pids(port)
  output = `#{find_pids_cmd(port)}`
  output.split("\n").map do |port|
    Integer(port)
  end
end

.running?(port) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/lsof.rb', line 8

def running?(port)
  listener_pids(port).empty? ? false : true
end

.running_remotely?(server, port) ⇒ Boolean

this isn’t really lsof, but it’s solving the same problem

Returns:

  • (Boolean)


13
14
15
16
# File 'lib/lsof.rb', line 13

def running_remotely?(server, port)
  TCPSocket.new(server, port).close rescue return false
  return true
end