Class: Procps::PS
- Inherits:
-
Object
- Object
- Procps::PS
- Defined in:
- lib/procps/ps.rb,
lib/procps/ps/base_columns.rb,
lib/procps/ps/extra_columns.rb,
lib/procps/ps/command_builder.rb
Overview
By default it loads only base columns. To load extra columns require procps/ps/extra_columns after the gem is loaded.
You can also define custom columns with the Procps::PS.define_column method.
Defined Under Namespace
Classes: CommandBuilder
Constant Summary collapse
- DEFAULT_BIN_PATH =
"/usr/bin/ps"- DEFAULT_COLUMNS =
i(pid rss pcpu)
- Address =
-> (base = 10, null: "-".freeze) { -> (v) { v.is_a?(String) ? v == null ? nil : v.to_i(base) : v } }
- Address_10 =
Address[]
- Address_16 =
Address[16]
Instance Attribute Summary collapse
-
#bin_path ⇒ Object
Returns the value of attribute bin_path.
-
#modifiers ⇒ Object
Returns the value of attribute modifiers.
-
#options ⇒ Object
Returns the value of attribute options.
Class Method Summary collapse
-
.alias_column(new_name, old_name) ⇒ Object
Creates an alias to a column.
- .columns ⇒ Object
-
.define_column(name, header = nil, cast = nil, &cast_block) ⇒ Object
Define a column (see base columns in
lib/procps/ps/base_columns.rband extra columns inlib/procps/ps/extra_columns.rb).
Instance Method Summary collapse
-
#columns ⇒ Object
List requested column objects with a typecast.
-
#initialize(bin_path = nil) ⇒ PS
constructor
Creates a Procps::PS object.
-
#limit(n) ⇒ Object
Limit processes list size.
-
#load(force = false) ⇒ Object
(also: #to_a)
Executes a ps command & sets a result.
-
#reset ⇒ Object
Reset a result.
-
#select(*columns) ⇒ Object
Select columns to list with ps command.
-
#sort(*orders) ⇒ Object
Set sorting option.
-
#sum ⇒ Object
Sum a CPU time for parent proccesses.
-
#take(n = 1) ⇒ Object
Limit processes list size & get result.
-
#where(command: nil, group: nil, user: nil, pid: nil, ppid: nil, sid: nil, tty: nil, real_group: nil, real_user: nil) ⇒ Object
Filter processes list with conditions.
-
#with_args(**args) ⇒ Object
Takes a hash of options to set custom ps arguments.
Constructor Details
#initialize(bin_path = nil) ⇒ PS
Creates a Procps::PS object. Takes an argument with a bin path to ps command.
32 33 34 35 36 |
# File 'lib/procps/ps.rb', line 32 def initialize(bin_path = nil) @bin_path = bin_path || DEFAULT_BIN_PATH = { o: [] } @modifiers = Set.new end |
Instance Attribute Details
#bin_path ⇒ Object
Returns the value of attribute bin_path.
29 30 31 |
# File 'lib/procps/ps.rb', line 29 def bin_path @bin_path end |
#modifiers ⇒ Object
Returns the value of attribute modifiers.
29 30 31 |
# File 'lib/procps/ps.rb', line 29 def modifiers @modifiers end |
#options ⇒ Object
Returns the value of attribute options.
29 30 31 |
# File 'lib/procps/ps.rb', line 29 def end |
Class Method Details
.alias_column(new_name, old_name) ⇒ Object
Creates an alias to a column
25 26 27 |
# File 'lib/procps/ps.rb', line 25 def self.alias_column(new_name, old_name) columns[new_name.to_sym] = old_name.to_sym end |
.columns ⇒ Object
13 14 15 |
# File 'lib/procps/ps.rb', line 13 def self.columns @@columns ||= {} end |
.define_column(name, header = nil, cast = nil, &cast_block) ⇒ Object
Define a column (see base columns in lib/procps/ps/base_columns.rb and extra columns in lib/procps/ps/extra_columns.rb)
19 20 21 22 |
# File 'lib/procps/ps.rb', line 19 def self.define_column(name, header = nil, cast = nil, &cast_block) header ||= name.to_s.upcase columns[name.downcase.to_sym] = Column.new(header, cast, &cast_block) end |
Instance Method Details
#columns ⇒ Object
List requested column objects with a typecast.
135 136 137 |
# File 'lib/procps/ps.rb', line 135 def columns @columns ||= [:o].map(&@@columns) end |
#limit(n) ⇒ Object
Limit processes list size
92 93 94 95 |
# File 'lib/procps/ps.rb', line 92 def limit(n) @limit = n self end |
#load(force = false) ⇒ Object Also known as: to_a
Executes a ps command & sets a result.
127 128 129 130 |
# File 'lib/procps/ps.rb', line 127 def load(force = false) reset if force @result ||= exec_command end |
#reset ⇒ Object
Reset a result
121 122 123 124 |
# File 'lib/procps/ps.rb', line 121 def reset @result = nil self end |
#select(*columns) ⇒ Object
Select columns to list with ps command
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/procps/ps.rb', line 39 def select(*columns) columns.each do |col| unless @@columns.include?(col) raise ArgumentError, "unknown column :#{col}, please add it manually to Procps::PS.columns." end [:o] << col end self end |
#sort(*orders) ⇒ Object
Set sorting option. Doesn’t supported by an original OSX ps (use with_args() method instead).
Example:
Procps::PS.new.select(:pid, :rss).sort("ppid", "-rss").to_a
115 116 117 118 |
# File 'lib/procps/ps.rb', line 115 def sort(*orders) ([:sort] ||= []).concat(orders) self end |
#sum ⇒ Object
Sum a CPU time for parent proccesses
86 87 88 89 |
# File 'lib/procps/ps.rb', line 86 def sum @modifiers << "S" self end |
#take(n = 1) ⇒ Object
Limit processes list size & get result
98 99 100 |
# File 'lib/procps/ps.rb', line 98 def take(n = 1) limit(n).to_a end |
#where(command: nil, group: nil, user: nil, pid: nil, ppid: nil, sid: nil, tty: nil, real_group: nil, real_user: nil) ⇒ Object
Filter processes list with conditions
Available options:
-
:command -
:group -
:user -
:pid -
:ppid -
:sid -
:tty -
:real_group -
:real_user
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/procps/ps.rb', line 62 def where( command: nil, group: nil, user: nil, pid: nil, ppid: nil, sid: nil, tty: nil, real_group: nil, real_user: nil ) [:C] = Array(command) if command [:g] = Array(group) if group [:u] = Array(user) if user [:p] = Array(pid) if pid [:ppid] = Array(ppid) if ppid [:s] = Array(sid) if sid [:t] = Array(tty) if tty [:G] = Array(real_group) if real_group [:U] = Array(real_user) if real_user self end |
#with_args(**args) ⇒ Object
Takes a hash of options to set custom ps arguments.
Example:
Procps::PS.new.select(:pid, :rss).with_args(m: true).to_a
106 107 108 109 |
# File 'lib/procps/ps.rb', line 106 def with_args(**args) .merge!(args) self end |