1

I would like to assign some code that will be run when R is killed, for instance, save(list=ls(),file="dump.RData"). I thought this would be by trapping signals, e.g. SIGTERM, as referred to in this post, but there's nothing about signals from the shell in ?conditions.

?conditions does mention user interrupts; you can e.g. catch a Ctrl-C with withCallingHandlers( Sys.sleep(10), interrupt=function (e){cat("I saw that.\n")} ), but this doesn't catch SIGTERM.

How can I do this?

Community
  • 1
  • 1
petrelharp
  • 4,829
  • 1
  • 16
  • 17
  • 1
    What I'm reading indicates that R is not notified when the process is killed with that signal. – IRTFM May 13 '15 at 18:48
  • @BondedDust Ah, right, I meant SIGTERM. Edited. But, if there's another signal it can trap, then that would do also. – petrelharp May 15 '15 at 18:17

1 Answers1

0

Indeed if you send SIGUSR1 to an R process, it will dump the workspace and stop. On Linux you can do that with

kill -USR1 Rpid

where Rpid is the process id of the R instance you want to stop. You can find it with pgrep for instance.

If R is running in a terminal, you can interrupt it with CTRL-Z and then type

kill -USR1 %
damienfrancois
  • 52,978
  • 9
  • 96
  • 110