0

In R, I am facing this unpleasant issue. I am not sure if it is a bug or how to avoid this behaviour.

Pressing ^C on R console (tested on Linux) will kill background jobs. See the following examples:

Example 1 - using pipe:

conn1 <- pipe("ts | tee LOG.txt", open = "wt") # ts is from package moreutils
sink(conn1)
sink(conn1, type = "message")

1
# Apr 16 23:17:43 [1] 1

2
# Apr 16 23:17:46 [1] 2

Every output goes to LOG.txt and console, with timestamp. Now press ENTER and Ctrl-C:

3
4
# you get no output because the pipe got killed!

Example 2 - running another R process in the background (used e.g. in doRedis/startLocalWorkers()):

system("R --slave -e 'while (TRUE) { A <- rnorm(100000); Sys.sleep(0.05) }'", wait = FALSE)
# Loading required package: stats

You can see the process running. Now press ENTER and Ctrl-C:

# Execution halted

Is this a feature or a bug? Definitely not desired. Also not usual (for example bash will also not kill background process with Ctrl-C). How do I make Ctrl-C to not kill background processes in R?

Tested on R version 3.6.0 (2019-04-26), CentOS Linux release 7.6.1810 (Core).

Tomas
  • 57,621
  • 49
  • 238
  • 373
  • Does this post answer part of your question?: https://superuser.com/questions/262942/whats-different-between-ctrlz-and-ctrlc-in-unix-command-line – davidnortes Apr 16 '20 at 21:57
  • 1
    @davidnortes not at all, how could that answer my question? I feel almost offended, that's just RTFM for beginners. Did you take the time to read my question? – Tomas Apr 16 '20 at 22:07
  • nothing farther from my intention. I did not only read your question. I also reproduced the code that you gave us in a LinuxMint 19.3 with R v3.6.3: exactly the same result that you obtained. I also even made the experiment with other scripts of my own. Same result. What do all of them have in common? crtl+c in linux terminal stops processes (thus 'id say that it isn't a bug but a feature of the linux terminal). How do you make Ctrl-C to not kill background processes in R? No idea (ergo my question: does this answer part of your question?) No offense intended towards anybody – davidnortes Apr 16 '20 at 22:27
  • @davidnortes *"crtl+c in linux terminal stops processes"* only foreground processes! Linux doesn't stop **background** processes with Ctrl-C! OK, I understand that you didn't do it intentionally, ... I was just worried as I have bad experience with people on StackExchange who suggest that your question is a duplicate without getting the time to read. – Tomas Apr 16 '20 at 22:28
  • Not at all. I clearly do not have the whole answer for your question (that's why I didn't even try to draft one), but as far as I know since I learnt R a few years ago, when you run it in the linux terminal, crtl+c, crtl+z and crtl+\ are going to have consequences (either they are going to stop the command or the whole R session). Here you can see a question related to this subject from 2012: https://stackoverflow.com/questions/9337825/how-can-i-stop-a-running-r-command-in-linux-other-than-with-ctrl-c . Good luck in your quest – davidnortes Apr 16 '20 at 22:52

0 Answers0