Fixing broken IO streams

From this page.

This is pretty cool. Say you've got a long-running process that you really should have put in screen or nohup, but were too short-sighted. I find myself in this position a lot. Suppose also that you compound your lack of wisdom by accidentally deleting the file that it was writing to, or some other how fuck up the streams, but it's still printing to stdout.

  1. Don't panic.
  2. Determine the process ID $p$.
  3. Check the file handles. ls -l /proc/$p$/fd. 1 is probably the stdout.
  4. Open gdb and bind to that process: gdb -p $p$
  5. Once it's bound, close the broken stream. p close(1). Should print sth like $1 = 0.
  6. Open a new stream. p creat(”/tmp/iamidiot”, “0600”). Should print sth like $2 = 1.
  7. Quit out of gdb with q.

That was fantastic.


Quick note: If creat didn't return 1, you can rename what it did return using p dup2(3, 1), which should return sth like $2 = 1. See this page on stackoverflow for details.

fixing_broken_io_streams.txt · Last modified: 2010/07/30 09:53 by jobriath
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki