Skip to content
Prev 306803 / 398506 Next

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

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):

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

            
Original query:
On Tue, 2 Oct 2012, Mike Miller wrote: