Permanently modify what the system does, and make it per-user so users can add their own logout (and maybe even login) scripts via some tweaks to some files in home.
There is so much conflicting information, but I think pam is a good first thing to try out. I’ve attempted to make something happen with pam by adding my script in /etc/pam.d and doing chmod a+x to make it usable but no joy came from that, not even when I try tor use pam_exec.so to run the thing I want. (I even tried commands directly rather than shell files, not sure if that’s kosher.)
I took some info from "Adding System Hooks" section here: https://wiki.ubuntu.com/LightDM and I managed to do something similar by adding the line:
session-cleanup-script= sh -c "espeak goodbye"
at my /etc/lightdm/lightdm.conf file. I don't know but, is it possible to make different lightdm configurations per user? Also, according to the link above, session-cleanup-script is executed after user's session ends, so possible it may cause problems if you try to modify user's files in the executed script.
EDIT: You need to restart lightdm to take effect (sudo systemctl restart lightdm).
Well I just discovered these stuff, I have no idea. If you can please share the script you use and I will take a look and if I can understand any of it, I may give some ideas (4 eyes are always better than 2 ) .