|
8 | 8 | include Sys |
9 | 9 |
|
10 | 10 | do_delete = (ARGV.include? 'delete' or ARGV.include? '--delete' or ARGV.include? '-d') |
| 11 | +do_kill = (ARGV.include? 'kill' or ARGV.include? '--kill' or ARGV.include? '-k') |
11 | 12 |
|
12 | 13 | lines = `VBoxManage list vms` |
13 | 14 | vms = lines.split(/\n/) |
|
26 | 27 | `#{cmd}` |
27 | 28 | end |
28 | 29 |
|
29 | | - sleep(1) |
30 | | - # ps x | grep VBoxHeadless | grep systemvm64template-4.4.0 | egrep -o '^\s*[0-9]+' | xargs kill |
31 | | - ProcTable.ps { |p| |
32 | | - next unless p.cmdline.include? "VBoxHeadless" |
33 | | - next unless p.cmdline.include? vm_name |
34 | | - # not all rubies / proctables expose ruid |
35 | | - if defined? p.ruid |
36 | | - # VBoxManage should only list _our_ vms, but just to be safe... |
37 | | - next unless p.ruid == Process.uid |
38 | | - end |
39 | | - |
40 | | - puts "kill -SIGKILL #{p.pid}" |
41 | | - begin |
42 | | - Process.kill("KILL", p.pid) |
43 | | - rescue => exception |
44 | | - puts exception.backtrace |
45 | | - end |
46 | | - sleep(5) |
47 | | - puts "kill -SIGTERM #{p.pid}" |
48 | | - begin |
49 | | - Process.kill("TERM", p.pid) |
50 | | - rescue => exception |
51 | | - puts exception.backtrace |
| 30 | + if do_kill |
| 31 | + sleep(1) |
| 32 | + # ps x | grep VBoxHeadless | grep systemvm64template-4.4.0 | egrep -o '^\s*[0-9]+' | xargs kill |
| 33 | + ProcTable.ps do |p| |
| 34 | + next unless p.cmdline.include? "VBoxHeadless" |
| 35 | + next unless p.cmdline.include? vm_name |
| 36 | + # not all rubies / proctables expose ruid |
| 37 | + if defined? p.ruid |
| 38 | + # VBoxManage should only list _our_ vms, but just to be safe... |
| 39 | + next unless p.ruid == Process.uid |
| 40 | + end |
| 41 | + |
| 42 | + puts "kill -SIGKILL #{p.pid}" |
| 43 | + begin |
| 44 | + Process.kill("KILL", p.pid) |
| 45 | + rescue => exception |
| 46 | + puts exception.backtrace |
| 47 | + end |
| 48 | + sleep(5) |
| 49 | + puts "kill -SIGTERM #{p.pid}" |
| 50 | + begin |
| 51 | + Process.kill("TERM", p.pid) |
| 52 | + rescue => exception |
| 53 | + puts exception.backtrace |
| 54 | + end |
52 | 55 | end |
53 | | - } |
| 56 | + end |
54 | 57 | end |
0 commit comments