Kanrisuru

ps

Get a list of process details running on a system in ruby.
linux manual

Basic Usage

require 'kanrisuru'
host = Kanrisuru::Remote::Host.new(host: '127.0.1.1', username: 'ubuntu', keys: ['~/.ssh/id_rsa'])

result = host.ps(user: 'root')
result.success?
true

result[0].user
'root'

result[0].command
"/lib/systemd/systemd --system --deserialize 32"

Parameters

Field Type Description
group string
array
integer
Select processes by group id, name or a list of groups.
user string
array
integer
Select processes by user id, name or a list of groups.
pid array
integer
Select processes by a single process id or list of process ids
ppid array
integer
Select processes by a single parent process id or list of parent process ids

Result

Returns an array of ProcessInfo structs.

ProcessInfo Fields

Field Type Description
uid integer User ID number
user string User name
gid integer Group ID number
group string Group name
ppid integer Parent process ID
pid integer Process ID
cpu_usage double CPU utilization
memory_usage double Memory size
stat string Multi-character process state.
priority integer Kernel scheduling priority
flags integer Flags as in long format F field
policy_abbr string Scheduling class of the process (abbreviated).
policy string Scheduling class of the process (abbreviated).
cpu_time string Cummulative cpu time "hh:mm:ss" format.
command double Command name.

Return Example

host.ps(ppid: 1).to_a
[
  [ 0] #<Struct:Kanrisuru::Core::System::ProcessInfo:0x00044200
    command = "/usr/sbin/blkmapd",
    cpu_time = "00:00:00",
    cpu_usage = 0.0,
    flags = 1,
    gid = 0,
    group = "root",
    memory_usage = 0.0,
    ppid = 1,
    pid = 446,
    policy = "SCHED_OTHER",
    policy_abbr = "TS",
    priority = 19,
    stat = "Ss",
    uid = 0,
    user = "root"
  >,
  [ 1] #<Struct:Kanrisuru::Core::System::ProcessInfo:0x00044318
    command = "/sbin/multipathd -d -s",
    cpu_time = "00:13:05",
    cpu_usage = 0.0,
    flags = 4,
    gid = 0,
    group = "root",
    memory_usage = 0.1,
    ppid = 1,
    pid = 554,
    policy = "SCHED_RR",
    policy_abbr = "RR",
    priority = 139,
    stat = "SLsl",
    uid = 0,
    user = "root"
  >,
  [ 2] #<Struct:Kanrisuru::Core::System::ProcessInfo:0x00044430
    command = "/usr/sbin/rpc.idmapd",
    cpu_time = "00:00:00",
    cpu_usage = 0.0,
    flags = 1,
    gid = 0,
    group = "root",
    memory_usage = 0.0,
    ppid = 1,
    pid = 599,
    policy = "SCHED_OTHER",
    policy_abbr = "TS",
    priority = 19,
    stat = "Ss",
    uid = 0,
    user = "root"
  >,
  [ 3] .. [24],
  [25] #<Struct:Kanrisuru::Core::System::ProcessInfo:0x00045d58
    command = "/usr/sbin/mysqld",
    cpu_time = "00:53:38",
    cpu_usage = 0.3,
    flags = 4,
    gid = 119,
    group = "mysql",
    memory_usage = 2.3,
    ppid = 1,
    pid = 232280,
    policy = "SCHED_OTHER",
    policy_abbr = "TS",
    priority = 19,
    stat = "Ssl",
    uid = 112,
    user = "mysql"
  >,
  [26] #<Struct:Kanrisuru::Core::System::ProcessInfo:0x00045e70
    command = "/usr/sbin/uuidd --socket-activation",
    cpu_time = "00:00:00",
    cpu_usage = 0.0,
    flags = 4,
    gid = 112,
    group = "uuidd",
    memory_usage = 0.0,
    ppid = 1,
    pid = 247616,
    policy = "SCHED_OTHER",
    policy_abbr = "TS",
    priority = 19,
    stat = "Ss",
    uid = 107,
    user = "uuidd"
  >,
  [27] #<Struct:Kanrisuru::Core::System::ProcessInfo:0x00045f88
    command = "/usr/lib/snapd/snapd",
    cpu_time = "00:00:05",
    cpu_usage = 0.0,
    flags = 4,
    gid = 0,
    group = "root",
    memory_usage = 0.2,
    ppid = 1,
    pid = 256325,
    policy = "SCHED_OTHER",
    policy_abbr = "TS",
    priority = 19,
    stat = "Ssl",
    uid = 0,
    user = "root"
  >
]

Exit Status

Code Description
0 Success
1 Failure

Tested On

  • Ubuntu, Debian, Centos, Fedora, Redhat, OpenSuse, SLES