Module: UnixPs

Defined in:
lib/unix-ps.rb,
lib/unix-ps/version.rb,
lib/unix-ps/unix_process.rb

Defined Under Namespace

Classes: UnixProcess

Constant Summary collapse

DELIM =

Subject to change, hopefully this does not cause issues

"!"
VERSION =
"0.0.8"

Class Method Summary collapse

Class Method Details

.processes(search = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/unix-ps.rb', line 11

def self.processes(search=nil)

  # Get ps aux output.
  #TODO: Handle errors
  ps_output = `ps aux`

  # Store the output in a temporary file so we can operate on the data without
  # losing consistency.
  file = Tempfile.new('unix-ps')
  file.write(ps_output)
  file.close

  # Lines to create process objects from
  lines = nil

  if search.is_a? String
    lines = `grep #{search} #{file.path}| awk '{print #{@columns}}'`.lines
    command_columns = `grep #{search} #{file.path}| awk '{#{@command_column}}'`.lines
    
    # Merge columns + command column
    lines = lines.each_with_index.map {|line, index|
      line = line.split(DELIM)
      command = command_columns[index].strip
      line.push(command)
    }
  else
    lines = `cat #{file.path} | awk '{print #{@columns}}'`.lines
    command_columns = `cat #{file.path}| awk '{#{@command_column}}'`.lines
    
    # Merge columns + command column
    lines = lines.each_with_index.map {|line, index|
      line = line.split(DELIM)
      command = command_columns[index].strip
      line.push(command)
    }

    # Pop off header columns
    lines.shift
  end

  file.unlink

  # Generate objects
  lines.collect { |line| UnixProcess.new(line) }
end