Skip to content

Instantly share code, notes, and snippets.

@sshao
Last active August 29, 2015 14:09
Show Gist options
  • Select an option

  • Save sshao/321a1942faaba7365da1 to your computer and use it in GitHub Desktop.

Select an option

Save sshao/321a1942faaba7365da1 to your computer and use it in GitHub Desktop.
lsof vs ruby's IO#closed?
$ ruby -v
ruby 1.8.7 (2012-02-08 patchlevel 358) [i686-darwin13.4.0]
# then we execute "ruby close_and_redirect.rb", and lsof at select points
# (see the two ruby scripts in this gist) during execution:
$ lsof -p 10623
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
ruby 10623 Sophia 0u CHR 16,3 0t1427 663 /dev/ttys003
ruby 10623 Sophia 1u CHR 16,3 0t1427 663 /dev/ttys003
ruby 10623 Sophia 2u CHR 16,3 0t1427 663 /dev/ttys003
# during the above, ruby reports "STDOUT closed?: false" ...ok
$ lsof -p 10623
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
ruby 10623 Sophia 0u CHR 16,3 0t1478 663 /dev/ttys003
ruby 10623 Sophia 2u CHR 16,3 0t1478 663 /dev/ttys003
# during the above, ruby reports "STDOUT closed?: true" ...ok
$ lsof -p 10623
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
ruby 10623 Sophia 0u CHR 16,3 0t1530 663 /dev/ttys003
ruby 10623 Sophia 2u CHR 16,3 0t1530 663 /dev/ttys003
# during the above, ruby reports "STDOUT closed?: false" !!
STDERR.puts "STDOUT closed?: #{STDOUT.closed?}"
STDERR.puts "sleeping... try lsof -p #{$$}\n"
sleep 10
STDOUT.close
STDERR.puts "STDOUT closed?: #{STDOUT.closed?}"
STDERR.puts "sleeping... try lsof -p #{$$}\n"
sleep 10
exec "ruby", "test_stdout.rb"
STDERR.puts "STDOUT closed?: #{STDOUT.closed?}"
STDERR.puts "sleeping... try lsof -p #{$$}"
sleep 10
puts "STDOUT says hello"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment