[R] R process must die - can I save history? [SOLVED]

Mike Miller mbmiller+l at gmail.com
Wed Oct 3 01:54:07 CEST 2012


This is a solution for UNIX/Linux-type OS users and a lot of it is only 
related to R in the sense that it can allow you to reconnect to an R 
session.  I managed to do it and to save history, thanks to helpful 
messages from Ista Zahn and Brian Ripley.

To explain this, I will call my two Linux boxes Desktop and Server.  I 
logged into Desktop, an Ubuntu box, and ran this command:

ps aux | grep ssh

That showed me these ssh processes and a few extraneous things:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
mbmiller  3520  0.0  0.0  46944  4668 pts/5    S+   Jul25   0:23 ssh -X Server
mbmiller  4602  0.0  0.0  47720  5544 pts/9    S+   Aug09   1:07 ssh -X Server
mbmiller 25614  0.0  0.0  45584  3344 pts/11   S+   Sep24   0:00 ssh -X Server

I launched an xterm from which I would try to recapture those ssh 
sessions.  (Spoiler: This worked for every ssh process.)  I was missing 
the reptyr binary, so I installed it like so:

sudo apt-get install reptyr

The reptyr man page told me that I had to do this to allow reptyr to work 
(I could change the 0 back to 1 after finishing):

$ sudo su
root# echo 0 > /proc/sys/kernel/yama/ptrace_scope
root# exit

After that I just ran reptyr for each process, for example:

reptyr 3520

A few lines of text would appear (the last saying "Set the controlling 
tty"), I'd hit "enter" and it would give my my command prompt from my old 
shell, or the R prompt if R was running in that shell.  I was then able to 
save command histories, etc.  When I tried to exit from R using q("yes") 
it accepted the command, but it did not return my bash prompt.  To deal 
with that, I tried Brian Ripley's recommendation:

I logged into Server via ssh and ran this command:

ps aux | grep R

Which returned this along with some irrelevant stuff:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
mbmiller  5156  0.0  0.1 213188  9244 pts/1    S+   Aug06   1:00 /share/apps/R-2.15.1/lib64/R/bin/exec/R -q

I tried the kill command...

kill -USR1 5156

...and that returned my bash prompt immediately in the other xterm.  R was 
done, the .Rhistory looked perfect, and .RData also was there.  I logged 
into Server, went to the appropriate directory, ran R and found that all 
of the objects were there and working correctly.

So that was amazing.  I could reattach to the R session and also kill it 
without losing history and data.  This is a big deal for me because I get 
stuck like that about once a year and it's always a huge pain.

Mike


On Tue, 2 Oct 2012, Ista Zahn wrote (off-list):

> If you can find the process ID you can try connecting the process to 
> another terminal with reptyr (https://github.com/nelhage/reptyr), and 
> then just use savehistory() as usual. You don't say what flavor of Linux 
> you're dealing with, but it looks like there are packaged versions for 
> at least Ubuntu, Fedora, and Arch.


On Tue, 2 Oct 2012, Prof Brian Ripley wrote:

> Maybe not.  On a Unix-alike see ?Signals.  If you can find the pid of 
> the R process and it is still running (and not e.g. suspended),
>
> kill -USR1 <pid>
>
> will save the workspace and history.



Original query:

On Tue, 2 Oct 2012, Mike Miller wrote:

> I connected from my desktop Linux box to a Linux server using ssh in an 
> xterm, but that xterm was running in Xvnc.  I'm running R on the server 
> in that xterm (over ssh).  Something went wrong with Xvnc that has 
> caused it to hang, probably this bug:
>
> https://bugs.launchpad.net/ubuntu/+source/vnc4/+bug/819473
>
> So I can't get back to that ssh session or to R.  I had done a bunch of 
> work in R but the command history hasn't been written out.  If I kill R, 
> I assume the command history is gone.  I wish I could somehow cause R to 
> dump the command history.  Is there any way to tell the running R 
> process to write the history somewhere?




More information about the R-help mailing list