Implement signal handler for SIGTERM.

Fixes #5087