class Knj::Unix_proc

This class handels various stuff regarding Unix-processes.

Constants

MUTEX
PROCS

Attributes

data[R]

Public Class Methods

find_self() click to toggle source

Returns the “Knj::Unix_proc” for the current process.

# File lib/knj/unix_proc.rb, line 96
def self.find_self
  procs = Knj::Unix_proc.list("ignore_self" => false)
  pid_find = Process.pid

  proc_find = false
  procs.each do |proc_ele|
    if proc_ele["pid"].to_i == pid_find.to_i
      proc_find = proc_ele
      break
    end
  end

  return proc_find
end
list(args = {}) { |data| ... } click to toggle source

Returns an array with (or yields if block given) Unix_proc. Hash-arguments as 'grep'.

# File lib/knj/unix_proc.rb, line 27
def self.list(args = {})
  cmdstr = "ps aux"
  grepstr = ""

  if args["grep"]
    grepstr = "grep #{args["grep"]}" #used for ignoring the grep-process later.
    cmdstr << " | grep #{Knj::Strings.unixsafe(args["grep"])}"
  end

  MUTEX.synchronize do
    ret = [] unless block_given?

    if args["psaux_str"]
      res = args["psaux_str"]
    else
      res = Knj::Os.shellcmd(cmdstr)
    end

    res.scan(/^(\S+)\s+([0-9]+)\s+([0-9.]+)\s+([0-9.]+)\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+ (.+)($|\n)/) do |match|
      pid = match[1]

      data = {
        "user" => match[0],
        "pid" => pid,
        "cpu_last" => match[2],
        "ram_last" => match[3],
        "cmd" => match[4],
        "app" => File.basename(match[4])
      }

      next if (!args.key?("ignore_self") or args["ignore_self"]) and match[1].to_i == $$.to_i
      next if grepstr.length > 0 and match[4].index(grepstr) != nil #dont return current process.

      if args.key?("pids")
        found = false
        args["pids"].each do |pid_given|
          if pid_given.to_s == pid.to_s
            found = true
            break
          end
        end

        next if !found
      end

      if args["yield_data"]
        yield(data)
      else
        proc_obj = Knj::Unix_proc.spawn(data)

        if block_given?
          yield(proc_obj)
        else
          ret << proc_obj
        end
      end
    end

    PROCS.clean

    if block_given?
      return nil
    else
      return ret
    end
  end
end
new(data) click to toggle source

Initializes various data for a Unix_proc-object. This should not be called manually but through “Unix_proc.list”.

# File lib/knj/unix_proc.rb, line 122
def initialize(data)
  @data = data
end
pid_running?(pid) click to toggle source

Return true if the given PID is running.

# File lib/knj/unix_proc.rb, line 112
def self.pid_running?(pid)
  begin
    Process.getpgid(pid)
    return true
  rescue Errno::ESRCH
    return false
  end
end
spawn(data) click to toggle source

Spawns a process if it doesnt already exist in the wrap-map.

# File lib/knj/unix_proc.rb, line 12
def self.spawn(data)
  pid = data["pid"].to_i

  begin
    proc_ele = PROCS.get!(pid)
    proc_ele.update_data(data)
  rescue Wref::Recycled
    proc_ele = Knj::Unix_proc.new(data)
    PROCS[pid] = proc_ele
  end

  return proc_ele
end

Public Instance Methods

[](key) click to toggle source

Returns a key from the data or raises an error.

# File lib/knj/unix_proc.rb, line 137
def [](key)
  raise "No such data: #{key}" if !@data.key?(key)
  return @data[key]
end
alive?() click to toggle source

Returns true if the process is still alive.

# File lib/knj/unix_proc.rb, line 180
def alive?
  return Knj::Unix_proc.pid_running?(@data["pid"].to_i)
end
kill() click to toggle source

Kills the process.

# File lib/knj/unix_proc.rb, line 143
def kill
  Process.kill("TERM", @data["pid"].to_i)
end
kill!() click to toggle source

Kills the process with 9.

# File lib/knj/unix_proc.rb, line 148
def kill!
  Process.kill(9, @data["pid"].to_i)
end
kill_ensure(args = {}) click to toggle source

Tries to kill the process gently, waits a couple of secs to check if the process is actually dead, then sends -9 kill signals.

# File lib/knj/unix_proc.rb, line 158
def kill_ensure(args = {})
  begin
    self.kill
    sleep 0.1
    return nil if !self.alive?

    args[:sleep] = 2 if !args.key(:sleep)

    0.upto(5) do
      sleep args[:sleep]
      self.kill!
      sleep 0.1
      return nil if !self.alive?
    end

    raise "Could not kill the process."
  rescue Errno::ESRCH
    return nil
  end
end
pid() click to toggle source

Returns the PID of the process.

# File lib/knj/unix_proc.rb, line 132
def pid
  return @data["pid"].to_i
end
to_h() click to toggle source

Hash-compatible.

# File lib/knj/unix_proc.rb, line 153
def to_h
  return @data.clone
end
update_data(data) click to toggle source

Updates the data. This should not be called manually, but is exposed because of various code in “Unix_proc.list”.

# File lib/knj/unix_proc.rb, line 127
def update_data(data)
  @data = data
end