lsof 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGES +6 -0
- data/README +2 -2
- data/Rakefile +1 -1
- data/lib/lsof.rb +10 -8
- data/spec/fixtures/test_server.rb +44 -0
- data/spec/lsof_spec.rb +25 -31
- metadata +45 -37
data/CHANGES
CHANGED
data/README
CHANGED
@@ -4,5 +4,5 @@ Functions include getting the pid and killing a process that listens on a partic
|
|
4
4
|
|
5
5
|
== Usage
|
6
6
|
Lsof.running?(6666) # returns true if there is the process that listens on port 6666
|
7
|
-
Lsof.kill(6666) # kill the
|
8
|
-
Lsof.
|
7
|
+
Lsof.kill(6666) # kill the processes that listens on port 6666
|
8
|
+
Lsof.listener_pids(6666) # returns the process ids for the process that listens on port 6666
|
data/Rakefile
CHANGED
data/lib/lsof.rb
CHANGED
@@ -1,21 +1,23 @@
|
|
1
1
|
class Lsof
|
2
2
|
class << self
|
3
3
|
def kill(port)
|
4
|
-
pid =
|
5
|
-
|
4
|
+
pid = listener_pids(port)
|
5
|
+
`#{find_pids_cmd(port)} | xargs kill 2>&1`
|
6
6
|
end
|
7
7
|
|
8
8
|
def running?(port)
|
9
|
-
|
9
|
+
listener_pids(port).empty? ? false : true
|
10
10
|
end
|
11
11
|
|
12
|
-
def
|
13
|
-
|
14
|
-
|
15
|
-
nil
|
16
|
-
else
|
12
|
+
def listener_pids(port)
|
13
|
+
output = `#{find_pids_cmd(port)}`
|
14
|
+
output.split("\n").map do |port|
|
17
15
|
Integer(port)
|
18
16
|
end
|
19
17
|
end
|
18
|
+
|
19
|
+
def find_pids_cmd(port)
|
20
|
+
"lsof -i tcp:#{port} | grep '(LISTEN)' | awk '{print $2}'"
|
21
|
+
end
|
20
22
|
end
|
21
23
|
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require "optparse"
|
2
|
+
require "rubygems"
|
3
|
+
require "eventmachine"
|
4
|
+
require "timeout"
|
5
|
+
|
6
|
+
$port = nil
|
7
|
+
$pid_file = nil
|
8
|
+
options ||= OptionParser.new do |o|
|
9
|
+
o.on('-p', '--port=PORT') do |port_from_user|
|
10
|
+
$port = Integer(port_from_user)
|
11
|
+
end
|
12
|
+
|
13
|
+
o.on('-f', '--pid_file=PID_FILE') do |pid_file_from_user|
|
14
|
+
$pid_file = pid_file_from_user
|
15
|
+
end
|
16
|
+
end
|
17
|
+
options.parse!
|
18
|
+
|
19
|
+
class NoopServer < EventMachine::Connection
|
20
|
+
end
|
21
|
+
|
22
|
+
def create_pid_file(pid_file)
|
23
|
+
File.open(pid_file, "w") do |file|
|
24
|
+
file.print Process.pid.to_s
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
EventMachine::run {
|
29
|
+
EventMachine::start_server "localhost", $port, NoopServer
|
30
|
+
|
31
|
+
create_pid_file "#{$pid_file}.pid"
|
32
|
+
fork do
|
33
|
+
create_pid_file "#{$pid_file}_1.pid"
|
34
|
+
sleep
|
35
|
+
end
|
36
|
+
fork do
|
37
|
+
create_pid_file "#{$pid_file}_2.pid"
|
38
|
+
sleep
|
39
|
+
end
|
40
|
+
|
41
|
+
Timeout.timeout(5) do
|
42
|
+
File.exists?("#{$pid_file}.pid") && File.exists?("#{$pid_file}_1.pid") && File.exists?("#{$pid_file}_2.pid")
|
43
|
+
end
|
44
|
+
}
|
data/spec/lsof_spec.rb
CHANGED
@@ -3,9 +3,10 @@ require "#{File.dirname(__FILE__)}/spec_helper"
|
|
3
3
|
describe Lsof do
|
4
4
|
include WaitFor
|
5
5
|
include FileUtils
|
6
|
-
attr_reader :port, :
|
6
|
+
attr_reader :port, :process_pids
|
7
7
|
before do
|
8
8
|
@port = 6666
|
9
|
+
@process_pids = []
|
9
10
|
end
|
10
11
|
|
11
12
|
after do
|
@@ -18,26 +19,17 @@ describe Lsof do
|
|
18
19
|
|
19
20
|
def start_process_using_port
|
20
21
|
Thread.start do
|
21
|
-
|
22
|
-
|
23
|
-
File.open("lsof.pid", "w") do |file|
|
24
|
-
file.print Process.pid.to_s
|
25
|
-
end
|
26
|
-
require "rubygems"
|
27
|
-
require "eventmachine"
|
28
|
-
EventMachine::run do
|
29
|
-
EventMachine::start_server "127.0.0.1", #{port}, EventMachine::Protocols::LineAndTextProtocol
|
30
|
-
end
|
31
|
-
'2>&1
|
32
|
-
CMD
|
33
|
-
`#{cmd}`
|
22
|
+
dir = File.dirname(__FILE__)
|
23
|
+
system("ruby #{dir}/fixtures/test_server.rb -p #{port} -f lsof")
|
34
24
|
end
|
35
25
|
wait_for do
|
36
26
|
Lsof.running?(port)
|
37
27
|
end
|
38
|
-
|
39
|
-
|
40
|
-
|
28
|
+
process_pids << Integer(File.read("lsof.pid"))
|
29
|
+
process_pids << Integer(File.read("lsof_1.pid"))
|
30
|
+
process_pids << Integer(File.read("lsof_2.pid"))
|
31
|
+
process_pids.compact!
|
32
|
+
process_pids.length.should == 3
|
41
33
|
end
|
42
34
|
|
43
35
|
def hide_error_stream
|
@@ -54,13 +46,15 @@ describe Lsof do
|
|
54
46
|
end
|
55
47
|
|
56
48
|
describe '.kill' do
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
49
|
+
describe "when there is only one process listening to the port" do
|
50
|
+
it "kills all processes associated with a provided port" do
|
51
|
+
start_process_using_port
|
52
|
+
hide_error_stream do
|
53
|
+
Lsof.kill port
|
54
|
+
end
|
55
|
+
wait_for do
|
56
|
+
!Lsof.running?(port)
|
57
|
+
end
|
64
58
|
end
|
65
59
|
end
|
66
60
|
end
|
@@ -76,15 +70,15 @@ describe Lsof do
|
|
76
70
|
end
|
77
71
|
end
|
78
72
|
|
79
|
-
describe ".
|
80
|
-
it "when there is a process listening on a port, returns the process id" do
|
73
|
+
describe ".listener_pids" do
|
74
|
+
it "when there is a process listening on a port, returns the process id in an Array" do
|
81
75
|
start_process_using_port
|
82
|
-
|
83
|
-
|
76
|
+
pids = Lsof.listener_pids(port)
|
77
|
+
pids.should == process_pids
|
84
78
|
end
|
85
79
|
|
86
|
-
it "when there is no process listening on a port, returns
|
87
|
-
Lsof.
|
80
|
+
it "when there is no process listening on a port, returns []" do
|
81
|
+
Lsof.listener_pids(port).should == []
|
88
82
|
end
|
89
83
|
end
|
90
|
-
end
|
84
|
+
end
|
metadata
CHANGED
@@ -1,56 +1,64 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.9.4
|
3
|
-
specification_version: 1
|
4
2
|
name: lsof
|
5
3
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.
|
7
|
-
date: 2007-12-28 00:00:00 -08:00
|
8
|
-
summary: A library utilizing the lsof unix command for process management.
|
9
|
-
require_paths:
|
10
|
-
- lib
|
11
|
-
email: corey@pivotallabs.com;brian@pivotallabs.com;
|
12
|
-
homepage: http://pivotallabs.com
|
13
|
-
rubyforge_project: pivotalrb
|
14
|
-
description: A library utilizing the lsof unix command for process management.
|
15
|
-
autorequire: lsof
|
16
|
-
default_executable:
|
17
|
-
bindir: bin
|
18
|
-
has_rdoc: true
|
19
|
-
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
-
requirements:
|
21
|
-
- - ">"
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: 0.0.0
|
24
|
-
version:
|
4
|
+
version: 0.2.0
|
25
5
|
platform: ruby
|
26
|
-
signing_key:
|
27
|
-
cert_chain:
|
28
|
-
post_install_message:
|
29
6
|
authors:
|
30
7
|
- Corey Innis + Brian Takita
|
31
|
-
|
8
|
+
autorequire: lsof
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-04-22 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: A library utilizing the lsof unix command for process management.
|
17
|
+
email: corey@pivotallabs.com;brian@pivotallabs.com;
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README
|
32
24
|
- CHANGES
|
25
|
+
files:
|
33
26
|
- Rakefile
|
27
|
+
- CHANGES
|
34
28
|
- README
|
35
29
|
- lib/lsof.rb
|
36
|
-
- spec/lsof_spec.rb
|
37
|
-
- spec/spec_helper.rb
|
38
30
|
- spec/spec_suite.rb
|
39
|
-
|
31
|
+
- spec/spec_helper.rb
|
32
|
+
- spec/fixtures/test_server.rb
|
40
33
|
- spec/lsof_spec.rb
|
34
|
+
has_rdoc: true
|
35
|
+
homepage: http://pivotallabs.com
|
36
|
+
post_install_message:
|
41
37
|
rdoc_options:
|
42
38
|
- --main
|
43
39
|
- README
|
44
40
|
- --inline-source
|
45
41
|
- --line-numbers
|
46
|
-
|
47
|
-
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: "0"
|
49
|
+
version:
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: "0"
|
55
|
+
version:
|
53
56
|
requirements: []
|
54
57
|
|
55
|
-
|
56
|
-
|
58
|
+
rubyforge_project: pivotalrb
|
59
|
+
rubygems_version: 1.1.0
|
60
|
+
signing_key:
|
61
|
+
specification_version: 2
|
62
|
+
summary: A library utilizing the lsof unix command for process management.
|
63
|
+
test_files:
|
64
|
+
- spec/lsof_spec.rb
|