unix-ps 0.0.2 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +7 -0
- data/lib/unix_ps.rb +35 -7
- data/lib/unix_ps/unix_process.rb +14 -17
- data/lib/unix_ps/version.rb +2 -2
- metadata +2 -4
- data/.gitignore +0 -34
- data/unix_ps.gemspec +0 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c71436fe45e3030d2418f0e4f22b6b1ecdf68294
|
4
|
+
data.tar.gz: ae9a563e360c008716e6383b39fea929b78db902
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d9a1dcb62db448e0c804ec38f26a7092efd25f76edeab5355a7cf5809ae62c882f64fc2f24a9f1ac82960a4d3e3337e4946e239c13e6ec8835e252cd2c1eabef
|
7
|
+
data.tar.gz: 3ca2ed3566a4efdcc41fafc22ee3cdc1b2dbbba986b0e823be5ca66585ae1d9d6ab6cd07263b6559c742a87df971bfd4c2c242fdf847a27abecb37a3b4a893e1
|
data/README.md
CHANGED
@@ -19,4 +19,11 @@ UnixPs.processes.first
|
|
19
19
|
=> #<UnixPs::UnixProcess:0x007fa553834758 @user="jason", @pid=54672, @cpu="100.5", @mem="0.6", @vsz="1115436", @rss="93692", @tty="??", @stat="R", @start=2014-06-04 00:00:00 -0700, @time="3162:49.67", @command="/Applications/Dropbox.app/Contents/MacOS/Dropbox\n">
|
20
20
|
```
|
21
21
|
|
22
|
+
Search for processes by name
|
23
|
+
```ruby
|
24
|
+
UnixPs.processes("mysql")
|
25
|
+
=> [#<UnixPs::UnixProcess:0x007f82649d7838 @user="root", @pid=19440, @cpu="0.0", @mem="0.0", @vsz="2445652", @rss="336", @tty="s001", @stat="S+", @start=2014-05-20 00:00:00 -0700, @time="0:00.02", @command="/bin/sh\n">, #<UnixPs::UnixProcess:0x007f82649d71f8 @user="root", @pid=19439, @cpu="0.0", @mem="0.0", @vsz="2461648", @rss="108", @tty="s001", @stat="S+", @start=2014-05-20 00:00:00 -0700, @time="0:00.01", @command="sudo\n">, #<UnixPs::UnixProcess:0x007f82649d6bb8 @user="jason", @pid=55626, @cpu="0.0", @mem="0.0", @vsz="2432784", @rss="628", @tty="s000", @stat="S+", @start=2014-06-05 11:16:00 -0700, @time="0:00.00", @command="grep\n">, #<UnixPs::UnixProcess:0x007f82649d64d8 @user="jason", @pid=55624, @cpu="0.0", @mem="0.0", @vsz="2433364", @rss="920", @tty="s000", @stat="S+", @start=2014-06-05 11:16:00 -0700, @time="0:00.00", @command="sh\n">]
|
26
|
+
|
27
|
+
```
|
28
|
+
|
22
29
|
|
data/lib/unix_ps.rb
CHANGED
@@ -1,25 +1,53 @@
|
|
1
|
+
require 'tempfile'
|
1
2
|
require './unix_ps/unix_process'
|
2
3
|
module UnixPs
|
3
4
|
|
4
5
|
# Subject to change, hopefully this does not cause issues
|
5
6
|
DELIM = "!"
|
6
|
-
|
7
|
-
|
7
|
+
@columns = (1..10).collect{|i| "$#{i}"}
|
8
|
+
@columns = @columns.join(" \"#{DELIM}\" ")
|
9
|
+
@command_column = "for(i=1;i<11;i++) $i=\"\";print $0"
|
8
10
|
|
9
11
|
def self.processes(search=nil)
|
10
12
|
|
11
|
-
|
13
|
+
# Get ps aux output.
|
14
|
+
#TODO: Handle errors
|
15
|
+
ps_output = `ps aux`
|
16
|
+
|
17
|
+
# Store the output in a temporary file so we can operate on the data without
|
18
|
+
# losing consistency.
|
19
|
+
file = Tempfile.new('unix-ps')
|
20
|
+
file.write(ps_output)
|
21
|
+
|
22
|
+
# Lines to create process objects from
|
23
|
+
lines = nil
|
12
24
|
|
13
25
|
if search.is_a? String
|
14
|
-
|
26
|
+
lines = `grep #{search} #{file.path}| awk '{print #{@columns}}'`.lines
|
27
|
+
command_columns = `grep #{search} #{file.path}| awk '{#{@command_column}}'`.lines
|
28
|
+
|
29
|
+
# Merge columns + command column
|
30
|
+
lines = lines.each_with_index.map {|line, index|
|
31
|
+
line = line.split(DELIM)
|
32
|
+
command = command_columns[index].strip
|
33
|
+
line.push(command)
|
34
|
+
}
|
15
35
|
else
|
16
|
-
|
36
|
+
lines = `#{file.path} | awk '{print #{@columns}}'`.lines
|
37
|
+
command_columns = `#{file.path}| awk '{#{@command_column}}'`.lines
|
38
|
+
|
39
|
+
# Merge columns + command column
|
40
|
+
lines = lines.each_with_index.map {|line, index|
|
41
|
+
line = line.split(DELIM)
|
42
|
+
command = command_columns[index].strip
|
43
|
+
line.push(command)
|
44
|
+
}
|
17
45
|
end
|
18
46
|
|
19
47
|
# Pop off header columns
|
20
|
-
|
48
|
+
lines.shift
|
21
49
|
|
22
50
|
# Generate objects
|
23
|
-
|
51
|
+
lines.collect { |line| UnixProcess.new(line) }
|
24
52
|
end
|
25
53
|
end
|
data/lib/unix_ps/unix_process.rb
CHANGED
@@ -6,25 +6,22 @@ module UnixPs
|
|
6
6
|
|
7
7
|
attr_accessor :user, :pid, :cpu, :mem, :vsz, :rss, :tty, :stat, :start, :time, :command
|
8
8
|
|
9
|
-
def initialize(
|
10
|
-
#
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
@
|
15
|
-
@
|
16
|
-
@
|
17
|
-
@
|
18
|
-
@
|
19
|
-
@
|
20
|
-
@
|
21
|
-
@
|
22
|
-
puts fields[8]
|
23
|
-
@start = Time.parse(fields[8])
|
24
|
-
@time = fields[9]
|
25
|
-
@command = fields[10]
|
9
|
+
def initialize(columns)
|
10
|
+
# Assign the columns
|
11
|
+
@user = columns[0]
|
12
|
+
@pid = columns[1].to_i
|
13
|
+
@cpu = columns[2].to_f
|
14
|
+
@mem = columns[3].to_f
|
15
|
+
@vsz = columns[4]
|
16
|
+
@rss = columns[5]
|
17
|
+
@tty = columns[6]
|
18
|
+
@stat = columns[7]
|
19
|
+
@start = Time.parse(columns[8])
|
20
|
+
@time = columns[9]
|
21
|
+
@command = columns[10]
|
26
22
|
end
|
27
23
|
|
24
|
+
# Kill a process
|
28
25
|
def kill!(sig=nil)
|
29
26
|
sig ||= 'INT'
|
30
27
|
Process.kill(sig, self.pid)
|
data/lib/unix_ps/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
1
|
module UnixPs
|
2
|
-
VERSION = "0.0.
|
3
|
-
end
|
2
|
+
VERSION = "0.0.4"
|
3
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: unix-ps
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Parraga
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-06-
|
11
|
+
date: 2014-06-13 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A Ruby wrapper around the unix tool ps.
|
14
14
|
email:
|
@@ -17,12 +17,10 @@ executables: []
|
|
17
17
|
extensions: []
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
|
-
- ".gitignore"
|
21
20
|
- README.md
|
22
21
|
- lib/unix_ps.rb
|
23
22
|
- lib/unix_ps/unix_process.rb
|
24
23
|
- lib/unix_ps/version.rb
|
25
|
-
- unix_ps.gemspec
|
26
24
|
homepage: https://github.com/Sovietaced/unix-ps
|
27
25
|
licenses: []
|
28
26
|
metadata: {}
|
data/.gitignore
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
*.gem
|
2
|
-
*.rbc
|
3
|
-
/.config
|
4
|
-
/coverage/
|
5
|
-
/InstalledFiles
|
6
|
-
/pkg/
|
7
|
-
/spec/reports/
|
8
|
-
/test/tmp/
|
9
|
-
/test/version_tmp/
|
10
|
-
/tmp/
|
11
|
-
|
12
|
-
## Specific to RubyMotion:
|
13
|
-
.dat*
|
14
|
-
.repl_history
|
15
|
-
build/
|
16
|
-
|
17
|
-
## Documentation cache and generated files:
|
18
|
-
/.yardoc/
|
19
|
-
/_yardoc/
|
20
|
-
/doc/
|
21
|
-
/rdoc/
|
22
|
-
|
23
|
-
## Environment normalisation:
|
24
|
-
/.bundle/
|
25
|
-
/lib/bundler/man/
|
26
|
-
|
27
|
-
# for a library or gem, you might want to ignore these files since the code is
|
28
|
-
# intended to run in multiple environments; otherwise, check them in:
|
29
|
-
# Gemfile.lock
|
30
|
-
# .ruby-version
|
31
|
-
# .ruby-gemset
|
32
|
-
|
33
|
-
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
34
|
-
.rvmrc
|
data/unix_ps.gemspec
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
$:.push File.expand_path("../lib", __FILE__)
|
3
|
-
require "unix_ps/version"
|
4
|
-
|
5
|
-
Gem::Specification.new do |s|
|
6
|
-
s.name = "unix-ps"
|
7
|
-
s.version = UnixPs::VERSION
|
8
|
-
s.platform = Gem::Platform::RUBY
|
9
|
-
s.authors = ["Jason Parraga"]
|
10
|
-
s.email = ["Sovietaced@gmail.com"]
|
11
|
-
s.homepage = "https://github.com/Sovietaced/unix-ps"
|
12
|
-
s.summary = %q{A Ruby wrapper around the unix tool ps.}
|
13
|
-
s.description = %q{A Ruby wrapper around the unix tool ps.}
|
14
|
-
|
15
|
-
s.files = `git ls-files`.split("\n")
|
16
|
-
s.require_paths = ["lib"]
|
17
|
-
end
|