Android execute script at logout
On a rooted device you can make use of init's on shutdown trigger:
# /etc/init/custom.rc

on shutdown
    exec - -- /path/to/executable

For more details on how to run an executable from init's .rc file, and what factors need to be taken care of, see my answer to How to run an executable on boot and keep it running?