Running code when a script exits

Ctrl+C, Ctrl+C kills the Virtual Machine. Ctrl+G, Q should exit in a way that the exit handler gets called, alternatively you can try kill $BEAM_PID from another terminal.