sigterm is an extremely simple package that provides a way to implement just these kinds of operations in response to SIGTERM: when the package is loaded, it will install a signal handler that prevents R from immediately exiting. Users can then periodically check whether a SIGTERM signal has been received and respond appropriately.
It has been a while since you asked and in the meantime someone solved this in a package:
https://github.com/atheriel/sigterm
From the README: