Run script when logged out?
Machints has got a sample script available here to write the logon and logoff events to a log file.

You basically need to write your script, give it execute rights and then add it to the LogoutHook event:


  sudo defaults write com.apple.loginwindow \ LogoutHook /usr/local/bin/logoutscript


For some more advanced script you can also have a look here

Alternative solution

Have a look at the launchd services. It's the built in service management system used by OSX. Some suggestions are to have launchd monitor a file, and using the logoutHook above, touch the file, which will trigger launchd to run. In the launchd job you can sleep the process for a few seconds to ensure that logout is completed.

Not the ideal solution, but could work.