Skip to content

Instantly share code, notes, and snippets.

@ryan5500
Created January 19, 2012 06:14
Show Gist options
  • Save ryan5500/1638307 to your computer and use it in GitHub Desktop.
Save ryan5500/1638307 to your computer and use it in GitHub Desktop.
use -ww option getting pid from ps with pty lib (because pty's terminal output width is narrow)
#!/usr/bin/env ruby
# conding: utf-8
#
# this script shows pid which contains 'some_process'
#
require 'pty'
require 'expect'
cmd = 'ssh username@somehost'
PTY.spawn(cmd) do |reader, writer|
reader.expect(/Password:/)
writer.puts('password')
reader.expect(/\]%/)
writer.puts('ps auxww')
ps_lines = reader.expect(/\]%/).first.split("\r\n")
ps_lines = ps_lines[2..ps_lines.size-2]
#puts ps_lines.inspect
val = ps_lines.select {|l| l =~ /some_process/ }
puts val.first.split(' ')[1]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment