class Grampus

  def self.kill(port)
    new.kill_at(port)
  end

  def kill_at(port)
    pids = processes_at(port)
    pids.each do |pid|
      Process.kill("QUIT", pid)
    end
    pids.each do |pid|
      sleep 0.01 while alive?(pid)
    end
    pids
  end

  def processes_at(port)
     pids = `lsof -t -i tcp:#{port} 2>/dev/null`
     pids.empty? ? [] : pids.lines.map { |l| l.to_i }
  end

  def alive?(pid)
    Process.getsid(pid) rescue false
  end
end

class Grampus

  def self.kill(port)
    new.kill_at(port)
  end

  def kill_at(port)
    pids = processes_at(port)
    pids.each do |pid|
      Process.kill("QUIT", pid)
    end
    pids.each do |pid|
      sleep 0.01 while alive?(pid)
    end
    pids
  end

  def processes_at(port)
     pids = `lsof -t -i tcp:#{port} 2>/dev/null`
     pids.empty? ? [] : pids.lines.map { |l| l.to_i }
  end

  def alive?(pid)
    Process.getpgid(pid) rescue false
  end
end

Use Process.getpgid to see if process is alive as getsid is only available from ruby 2.0.0 onwards
Use Process.getpgid to see if process is alive as getsid is only
available from ruby 2.0.0 onwards
