I am trying to write a sort of daemon that keeps track of files and updates a database backend whenever something changes. This will be run in a docker container. Although i don't think it is crucial, it would be nice to be able to "tidy up" things whenver the container terminates. Terminating a Docker container with ex. Docker kill sends the TERM signal to the container process, e.g my daemon. The question is, is there a way to check for this signal in R and do a little cleanup before exiting?