The Emacs server
The best way to do this is to contact the Emacs server and run an Emacs Lisp command.
emacsclient -e '(with-current-buffer "*scratch*" (write-file "~/scratch.txt"))'
Or you could attach Emacs to the current terminal and do whatever you want there, such as switch to the `scratch buffer and save its contents.
emacsclient -nw
Unfortunately for you, you need to start the server explicitly. I recommend that you add (server-start)
to your init file, because it is very useful, but if you haven't done that already, it won't help you now.
Sending input to Emacs's terminal
When you run echo "abd" > /proc/$emacs_pid/fd/0
, this sends the text to the terminal that Emacs is running on. Writing data to a terminal displays that data. You can't send input to a terminal: input is what comes from a terminal.
Screen
If you run a program in a screen or tmux session, you can attach to the existing session and continue interacting with the program. Both screen and tmux also support programmatically sending input to a program running inside. This requires having run the program in the first place, though, so it won't help you this time.
With Emacs, Screen won't buy you much over connecting to the Emacs server with emacsclient
.
Core dump
Sifting through core dump is an option. Emacs stores the content of a buffer in a single chunk, with a gap in the middle (the gap may contain arbitrary junk; if the last operation was a deletion, it's what was deleted).
The SIGQUIT signal causes a process to die a leave a core dump (if enabled) by default. However, Emacs catches SIGQUIT (it quits to the toplevel, which is convenient in some situations but doesn't help you). So instead you'll need to attach to it with a debugger and make it dump core. This has benefits over SIGQUIT anyway: it leaves the process running and works even if core dumps are disabled.
$ gdb -p $(pidof emacs)
(gdb) gcore
You can then sift through the core dump to extract the data
Reattach Emacs to a new terminal
There's no Unix or even Linux feature to detach a program from one terminal and attach it on another. However, it is possible to attach a debugger to any program and make it open a different terminal. This can be hit-and-miss: sometimes it works, sometimes it crashes the program. There are several tools that attempt to do this as best as they can; see How can I disown a running process and associate it to a new screen shell?
cat /proc/sys/kernel/core_pattern
show? Some distributions set up core dumps differently, e.g. Ubuntu's apport. Regarding your second comment: Emacs's input comes from its stdin, which is the terminal; but when you write to the terminal, that doesn't become input for Emacs: it becomes input for the terminal emulator, which it displays. You'd need to provide output to the terminal emulator which it would translate as input for the program running in it. – Gilles 'SO- stop being evil' Dec 29 '14 at 12:00/var/core
? – Gilles 'SO- stop being evil' Dec 29 '14 at 22:01gcore
command. Regarding the input/output, don't think of/proc/PID/fd/0
as “the standard input of Emacs”, but as “the file that Emacs has open on its standard input”./proc/PID/fd/0
doesn't bypass the terminal: it is the terminal, same as/proc/PID/fd/1
. – Gilles 'SO- stop being evil' Dec 29 '14 at 22:15