How do I execute code at termination?

You can specify an action for the <<EOF>> rule.