Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@thewoolleyman
Last active January 16, 2017 05:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thewoolleyman/6a060574f22eafd42955812a1a2a7842 to your computer and use it in GitHub Desktop.
Save thewoolleyman/6a060574f22eafd42955812a1a2a7842 to your computer and use it in GitHub Desktop.
thewoolleyman-ruby-pty-check
#!/usr/bin/env ruby
require 'pty'
def process_running?(pid, loop_until_not_alive = false)
loop do
Process.getpgid(pid)
puts "Process with pid #{pid} is running"
break unless loop_until_not_alive
sleep 0.5
end
rescue
puts "Process with pid #{pid} is NOT running"
end
def process_check(pid, loop_until_not_alive = false)
loop do
status = PTY.check(pid)
puts "PTY::check for process with pid #{pid} is : #{status.inspect}"
if loop_until_not_alive && !status
sleep 0.5
else
break
end
end
end
def output_only_example
puts "Simple output-only example:\n"
cmd = 'printf "f"'
puts "cmd: #{cmd}"
r, _, pid = PTY.spawn(cmd)
process_running?(pid) # Process is NOT running...
process_check(pid) # But STDOUT has not yet been read, so check returns nil
puts r.gets(1)
process_check(pid, true) # After STDOUT has not yet been read, check (soon) returns Process::Status
end
def invalid_command_example
puts "\n\ninvalid command example:"
cmd = 'exit 42'
puts "cmd: #{cmd}"
output_buffer = StringIO.new('')
PTY.spawn(cmd) do |r, w, pid|
sleep 0.5 # to give command time to run
# it will be dead by now
process_running?(pid, true)
process_check(pid, true)
puts output_buffer.string
end
end
def input_output_example
puts "\n\ninput + output example - exits when given 'z' on STDIN:"
cmd = %q(ruby -e 'require "io/console"; while(i=$stdin.getch) do puts ">#{i}<"; if i =~ /z/; break; end; end')
puts "cmd: #{cmd}"
output_buffer = StringIO.new('')
PTY.spawn(cmd) do |r, w, pid|
sleep 0.5 # to give command time to run
# it's alive
process_running?(pid)
process_check(pid)
puts 'writing "a"'
w.putc 'a'
puts 'reading...'
a = r.read(3)
output_buffer.puts(a)
puts 'output_buffer contents:'
puts output_buffer.string
# still alive
process_running?(pid)
process_check(pid)
puts 'writing "z"'
w.putc 'z'
sleep 0.5
puts 'reading...'
z = r.read(3) # WHY DOESN'T THIS READ '>z<', LIKE IT DOES FROM A REAL TERMINAL?
sleep 0.5
output_buffer.puts(z)
puts 'output_buffer contents:'
puts output_buffer.string
# why isn't it dead?
process_running?(pid)
process_check(pid)
puts output_buffer.string
end
end
# output_only_example
# invalid_command_example
input_output_example
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment