It depends on the method used to exit manually. If the user can terminate the script using Ctrl-C, you can trap the signal sent to the script and execute whatever you want. The same if the EXIT signal (not interruption) is sent to the script. See these two
examples on the Advanced Bash User Guide to get the idea.
Edit: Beaten by catkin...