[Rd] Capturing interrupts during the execution of system/system2

Gaidatzis, Dimosthenis Dimosthenis.Gaidatzis at fmi.ch
Thu Jan 19 12:56:57 CET 2012


Dear R Team

I have a question about the system/system2 command in R on linux. My goal is to run a system command (which can take a long time) and to detect if it was successful or not. If i understood correctly the return value of system should give me exactly this information. However if i try to do this, system returns 0, even if i interrupt the execution with CTRL-C (which is the event i would like to detect). Here is an example (just piping /dev/zero to /dev/null) to illustrate the behavior after pressing CTRL-C.

> ret <- system("cat /dev/zero > /dev/null"); print(ret)
^C[1] 0

I also tried to use a tryCatch block to detect this event but pressing CTRL-C during the execution of the system command does not result in a capture of this interrupt event. Only releasing resources is called which happens in any case:

> tryCatch({
+   ret <- system("cat /dev/zero > /dev/null"); print(ret)
+ }, interrupt = function(ex) {
+   cat("An interrupt was detected.\n");
+   print(ex);
+ }, error = function(ex) {
+   cat("An error was detected.\n");
+   print(ex);
+ }, finally = {
+   cat("Releasing resources...");
+   cat("done\n");
+ })
^C[1] 0
Releasing resources...done


Is there still a way to find out if the user stopped the R script while the system command was running?

R version 2.14.1 (2011-12-22)
Platform: x86_64-unknown-linux-gnu (64-bit)

Kind regards
Dimos Gaidatzis



More information about the R-devel mailing list