Save scratch emacs file without access to terminal

emacstty

I'm trying to save the contents of an Emacs scratch buffer, which I can't access any more due to an inaccessible terminal.

On my Linux box I've ssh'ed to a server, and started Emacs. My Linux box has now frozen, however I can still see the Emacs process alive on the server ssh'ed into.

Is there a way to get Emacs to dump/save the contents of its scratch without having direct access? I had two thoughts:

  1. Send a signal to the process so that Emacs dumps its core, and then reload the core (and then save scratch)
  2. Send keystrokes to the stdin of the process which would instruct the Emacs process to save the file, ie: via echo "abd" > /proc/<pid>/fd/0.
    I tried this by opening two terminals, and the keystrokes appear on the target terminal, however they aren't captured by Emacs.

Best Answer

I've had some luck with attaching gdb to the running process. Borrowing heavily from: How to attach terminal to detached process?

write-file

  1. mkfifo /tmp/some_name
  2. gdb -p [pid]
  3. (within gdb): call close(0)
  4. (within gdb): call open('/tmp/some_name', 0600). At this point gdb will appear to hang
  5. (from the shell): echo '(write-file "savedresults")' > /tmp/some_name
  6. (within gbd): ctrl-d

The contents of scratch are written out to the file 'savedresults'. Interestingly (write-file "savedresults") is appended to the file (unsure why).

C-x C-w name

  1. mkfifo /tmp/some_name
  2. gdb -p [pid]
  3. (within gdb): call close(0)
  4. (within gdb): call open('/tmp/some_name', 0600). At this point gdb will appear to hang
  5. (from the shell): cmd="^X^Wsavedresults" (input ^X via key sequence: CTRL-V CTRL-X, similar for ^W"
  6. (from the shell): echo "$cmd" > /tmp/some_name
  7. (within gbd): ctrl-d

Contents are written to file 'savedresults'.

Related Question