OSX - User Log Out - Error Trapping

Does anybody have a snippet/routine to trap a “on log out” type of error? I have this background running script that I’d like to make a graceful exit upon logout/restart/shutdown.

I found my own problem. I had an “error level -128” in my quit routine. It seems that is not a good thing to have inside the quit.