I have a R script that I have to 'kill' sometimes to stop its execution. But on 'killing' it, the code that does the cleanup(closing connections, etc.) is not executed. How can I make sure the cleanup code is executed no matter how the script is stopped.
My script is myTest.R:
print("script called")
Sys.sleep(20)
.Last<-function()
{
print("Last called")
}
I start the R script from command line:
> Rscript myTest.R
If I wait for 20 seconds, the .Last function is called.
Now sometimes I have to interrupt the execution of script by calling:
kill -USR1 pid
where pid is the process id. This call is documented here: http://stat.ethz.ch/R-manual/R-devel/library/base/html/Signals.html
This kills the process but the .Last function is not called. but the documentation says it should be called. What am I missing here. Help please. Oh and I am running it on Linux.
Thanks