Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background R console and long running session

Tags:

emacs

ssh

r

this is my situation: I usually run R from within Emacs using ESS into terminal emulator, in my local pc. In my work place we get a new server running R so I would use the remote server via ssh. I connect via ssh and all works well. What I would do is to keep alive the R console while I close my laptop and go home so, from my home I would reconnect to the existing R session. I tried to put the R console in background using C-q C-z Enter to stop the process but, while I close the ssh connection the proces is killed. No luck using bg & too. I also tried mosh but, also in this case, I get some issue related to the UDP traffic across my work's network. Screen and tmux are not also very useful due to their bad interaction with the Emacs eshell. In both client and server machine I run Debian 8 xfce.

Is there a way to keep alive the R terminal while closing the ssh connection? Which is your approach to the long R sessions?

EDIT

Finally here and here I found the solutio that I'm looking for. I tried the same approach as in the link above, but using tmux, and I get lots of error. The holy grail is screen. I tried to follow step-by-step that procedure but I get an error from emacs while I try to attach a screen session from within eshell. So I tried to use ansi-term instead of eshell and all works as expected. I can attach and detach the R session. In this way I use the remote server machine only for the computation while the R scripts are in my laptop. So, this is the work-flow:

  1. ssh to the host server
  2. start screen session
  3. start R
  4. detach screen
  5. exit from the server closing the ssh connection
  6. run emacs as daemon in your local machine and open an emacsclient instance (not necessary run emacs via emacsclient but I prefer this way)
  7. open your R script
  8. open an ansi-term (M-x ansi-term)
  9. ssh to the server from ansi-term
  10. attach the screen session (screen -r)
  11. connect the remote R console to the local R script (M-x ess-remote)
  12. to detach from R from within ansi-term use Ctrl-q Ctrl-a d return

Thats it. Now I can run a remote R process using a local R script, closing the connection but leaving open the R console so I can re-attach to it in the future, also from a different IP.

like image 822
Riccardo Avatar asked Nov 12 '15 15:11

Riccardo


1 Answers

This is one of my favourite topics :) Here is what I do:

  • Always start emacs as emacs --daemon so that it runs in the background.
  • Always launch emacsclient -nw (for textmode) or emacsclient -c (in x11/graphical mode) to access the daemonized emacs in the background. I have these aliased to emt and emx, respectively.

Now you are essentially done. You can ssh to that box and resume from whereever you can launch ssh from---which may be a smartphone or browser. And ESS of course allows you to have multiple R sessions. After M-x R I often invoke M-x rename-buffer to align the buffer with the project name or idea I work on.

I combine this further with both

  • byobu (which is a fancy tmux wrapper available in many distros and on OS X, and originally from Ubuntu) to have shell sessions persist
  • mosh for places like work and home where my laptop can simply resume

Strictly speaking you do not need byobu or mosh for emacs to persist (as running the daemon takes care of that) but you may want it for all your other shell session.

This setup has been my goto tools for years at work and home.

like image 86
Dirk Eddelbuettel Avatar answered Sep 21 '22 20:09

Dirk Eddelbuettel