On Ubuntu, is there a way to capture the content of a pseudoterminal (pts - eg. /dev/pts/4) - ie. dump the lines of text in the terminal to a file - of a program that's already running in that terminal? I'm not talking about taking a screenshot, but storing what the program is writing to the terminal to a file. It's a curses-based program, that regularly rewrites the screen.
My main problem, is that my program is already running - and I'd like to store this before stopping (and perhaps restarting) the program.
I know there are several ways to accomplish this is I'd thought about this possibility before I started the program: Simply redirecting the output to a file; using screen, and taking a hardcopy or logging the session; using a virtual terminal instead (/dev/tty..) and using the video console memory device (/dev/vcs..); using the command script... (and probably many others...)
But all of this, would only have worked if I did it before I started the program!
So, is there still a way I could "snoop" on the pseudoterminal and record it's content? I can become root if I have to...
21 Answer
Maybe not directly from the tty, but this processus has to use the write syscall. You can use strace to catch all write calls.
sudo strace -e t=write -s 6000 -p your-process-id 2>logfileYou will get things like this, that might need some more parsing :
write(1, "191\n", 4) = 4
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=9916, si_uid=1001, si_status=0, si_utime=0, si_stime=0} ---
write(1, "21021\n", 6) = 6
write(1, "14728\n", 6) = 6
write(1, "13291\n", 6) = 6
write(1, "20372\n", 6) = 6If your program writes to some other files, you'll get that too in the output. That can be annoying. Or that eventually could be a huge amount of data. The file descriptor used is probably 1. You can grep "^write.1"
sudo strace -e t=write -s 6000 -p your-process-id 2>&1 | grep ^write.1 >logfileNext time, try to run you command with script. It catches all data written to the tty.
script -c "program and args" logfile