I've discovered some version of this already exists as I encountered it when running this:
timeout -v --preserve-status -k 1m 5280s rclone sync --transfers=10 --max-duration=58m --retries 1 --fast-list --gphotos-include-archived --log-level INFO --log-file=REDACTED.txt gphotos:album album
~25 minutes after
--max-duration was reached (elapsed time was 1h23m), rclone gave up on a connection that disappeared and deleted the partially downloaded file.
2021/03/09 01:52:55 NOTICE: REDACTED/VID_20190828_054220.mp4: Removing partially written file on error: context deadline exceeded
2021/03/09 01:52:55 INFO : can't close account: file already closed
2021/03/09 01:53:01 INFO :
Transferred: 15.177G / 15.177 GBytes, 100%, 3.275 MBytes/s, ETA 0s
Errors: 6 (retrying may help)
Checks: 81283 / 81283, 100%
Transferred: 572 / 1440, 40%
Elapsed time: 1h23m0.0s
* REDACTED 1…ID_20190825_153918.mp4: 0% /off, 0/s, -
* REDACTED 1…ID_20190826_083814.mp4: 0% /off, 0/s, -
* REDACTED 1…ID_20190826_165940.mp4: 0% /off, 0/s, -
* REDACTED 1…ID_20190827_073318.mp4: 0% /off, 0/s, -
* REDACTED 1…ID_20190829_041405.mp4: transferring
This helps my original issue if I can figure out how to speed up up the "context deadline exceeded".
But I think this doesn't address the reason I've requested this feature. I think it still makes sense to have the option that if SIGTERM is received by rclone this code should be called before exit (system could be shutting down or process could be getting evicted).