Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm opening terminal window to server with Putty or some other terminal emulator. I usually work with Putty and use x11 forwarding. Putty create .Xauthority file if one is missing. I connect to server with my username, then connect with sudo - oracle to oracle account on the same server. I have to bring X credentials to oracle session. I can do it with xauth command. It works. But when I leave session I wish to clean .Xauthority in oracle session and in my user session. If using exit from shell I can use .bash_logout to put some code in there to clean up. But when I close window this method is not working.
Is there a way to run a program before terminal closes with Alt-F4 or click on close window button.
No, by doing ALT-F4 or closing the window you are destructively ending any sessions you have. I don't see how you can monitor that unless you rewrite the server providing the session and put a signal handler in there to cover the signal cases which occur when these events happen. Probably SIGTERM if not some other ones and probably also SIGTERM under certain status return conditions, because closing normally probably also results in a SIGTERM. Either case, normal closure or destructive one there's no harm in trying to delete something that's no longer there, simply results in a minor error.
Would it be possible to close your session using logout or exit?
I guess in this case the bash_logout should be called.
Alternatively, you could have your server do a clean-up using a crontab task every hour or something.
No, by doing ALT-F4 or closing the window you are destructively ending any sessions you have. I don't see how you can monitor that unless you rewrite the server providing the session and put a signal handler in there to cover the signal cases which occur when these events happen. Probably SIGTERM if not some other ones and probably also SIGTERM under certain status return conditions, because closing normally probably also results in a SIGTERM. Either case, normal closure or destructive one there's no harm in trying to delete something that's no longer there, simply results in a minor error.
Thank you, rtmistler!
Your answer is quite educational. Because of your first one syllable word, I wrote some code in .bash_login, bashrc and bash_logout. If I'm behaving nice and play by the rule all works good. I can jump from server to server and clean at leaving the session. I'll write exit in terminal instead of clicking on little X in the corner of window.
Thanks a lot, but in some future time I'll nag you about this SIG-things.
Also there is the shortcut Ctrl-D (or Cmd-D) to close the terminal. This issues exit and is therefore compatible with your .bash_logout.
Yes, good point that CTRL-D causes a logout action.
OP the signals I was referring too would be a very complex solution, not something with settings or even a script, but instead re-writing code that runs the terminal or XWindow itself. Unless you're up for that it would be a highly difficult path to a solution when the better options are to follow normal guidelines of using exit or as above, CTRL-D.
Would it be possible to close your session using logout or exit?
I guess in this case the bash_logout should be called.
Alternatively, you could have your server do a clean-up using a crontab task every hour or something.
Hi, hoes
Thanks for concerning. I'm doing exactly as you propose. It works
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.