38

I'm running R in interactive mode under Linux and mistakenly typed in a command that takes forever to run. I usually stop it using Ctrl + C. But it doesn't work all the time. When it doesn't work, is there another way?

I don't want to kill the R session to start over either.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
danioyuan
  • 1,268
  • 2
  • 13
  • 16

4 Answers4

39

I have had the problem you mention. Ctrl + C will work when R is able to realize "Oh, this guy/gal wants me to stop running that particular command." However, often R cannot do that. So you have to run Ctrl + \ (note that it's a backslash, not a forward slash). This tells Linux "Hey, R is not listening to me, can you stop running R?".

Try Ctrl + C first, because if it works you will still have your R session. If it doesn't work, and you do Ctrl + \ you will lose your R session, but at least it stops the process.

If that doesn't work either, then I would suggest a killall R or a kill -9 [PID] where you find the PID by running pus aux.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Xu Wang
  • 10,199
  • 6
  • 44
  • 78
  • 8
    When Ctrl+C doesn't work, it's usually because you have called a function that calls compiled code that doesn't check for user interrupt. These are usually in user-supplied packages, not base/recommended R packages. – Joshua Ulrich Feb 18 '12 at 12:21
  • Thanks for the response, although I was looking for a solution that can keep the R session alive. – danioyuan Feb 21 '12 at 23:20
  • @danioyuan, from what I gather from Joshua's comment, that may not be possible with some of the functions you are calling - they are free to ignore user interrupts. – Richard Jul 03 '12 at 17:08
10

Try to use Ctrl + C, pause R with Ctrl + Z, and then unpause R with fg (#job).

I tried the solutions given in previous answers with sending some signals to R with kill, but neither worked. Then I tried the sequence above (Ctrl + C, Ctrl + Z, and fg 1) and it worked. I could continue the R session.

I'm not sure whether it was one of the signals or Ctrl + C that stopped R, but I guess Ctrl + Z was essential here.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Oberyn
  • 101
  • 1
  • 2
8

You can also try

kill -USR1 [pid]

This will interrupt some R calls and sometimes give you the option to save and quit rather killing the process altogether.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mike
  • 81
  • 1
  • 1
3

Ctrl + C is not working. Somehow Ctrl + \ will stop the process.

But there is a function in R programming to quit. You can try q() on REPL. It worked for me.

q()
xdeepakv
  • 7,835
  • 2
  • 22
  • 32