LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 03-16-2015, 09:34 AM   #1
franjo124
LQ Newbie
 
Registered: Mar 2008
Posts: 14

Rep: Reputation: 0
Run program before close terminal


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.

Regards
Franjo
 
Old 03-16-2015, 12:14 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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.
 
Old 03-17-2015, 04:50 AM   #3
hoes
Member
 
Registered: Sep 2005
Distribution: debian, linux from scratch
Posts: 190

Rep: Reputation: 51
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.
 
Old 03-17-2015, 08:06 AM   #4
franjo124
LQ Newbie
 
Registered: Mar 2008
Posts: 14

Original Poster
Rep: Reputation: 0
Talking

Quote:
Originally Posted by rtmistler View Post
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, bash​rc 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.

Regards, Franjo
 
Old 03-17-2015, 08:15 AM   #5
hoes
Member
 
Registered: Sep 2005
Distribution: debian, linux from scratch
Posts: 190

Rep: Reputation: 51
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.
 
Old 03-17-2015, 08:25 AM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by hoes View Post
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.
 
Old 03-17-2015, 09:19 AM   #7
franjo124
LQ Newbie
 
Registered: Mar 2008
Posts: 14

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by hoes View Post
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

Regards, Franjo
 
Old 03-17-2015, 09:27 AM   #8
franjo124
LQ Newbie
 
Registered: Mar 2008
Posts: 14

Original Poster
Rep: Reputation: 0
Thumbs up

Quote:
Originally Posted by hoes View Post
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.
Hi, hoes,
That is exactly what I needed. I'm no mouse clicking enthusiast, more keyboard fan!

Thanks, again to all.

Regards, Franjo
 
  


Reply

Tags
close, program, run, terminal



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] need bash script to close terminal when program closes rrrssssss Programming 5 09-25-2013 03:17 PM
[SOLVED] Program closes fine when run from cmd but .jar doesn't close. GanglyCloth Programming 1 07-17-2012 02:03 AM
LXer: How to close the terminal without killing the running program LXer Syndicated Linux News 0 06-15-2012 03:50 PM
How to run a program in an ssh session that will run after you close out the session? dr_zayus69 Linux - Networking 5 03-05-2006 07:15 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration