Skip to content

R process must die - can I save history?

6 messages · Bert Gunter, Brian Ripley, steven mosher +2 more

#
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?

Thanks in advance.

Mike

--
Michael B. Miller, Ph.D.
Minnesota Center for Twin and Family Research
Department of Psychology
University of Minnesota
#
?history

in a fresh R session, to see what might be possible. I'll bet the
answer is, "No, you're screwed," though. Nevertheless, maybe Linux
experts can save you.

May the Force be with you.

-- Bert
On Tue, Oct 2, 2012 at 10:17 AM, Mike Miller <mbmiller+l at gmail.com> wrote:

  
    
#
On 02/10/2012 18:29, Bert Gunter 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.

  
    
#
Agreed -- very cool trick. Thanks Prof Ripley

Michael
On Oct 2, 2012, at 9:59 PM, steven mosher <moshersteven at gmail.com> wrote:

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