[Tfug] Properly terminating a shell script after using X Windows

Jeff Breadner jeff at breadner.ca
Fri Jan 25 10:31:48 MST 2013


On Fri, Jan 25, 2013 at 3:44 AM, John Gruenenfelder
<jetpackjohn at gmail.com> wrote:
> Hello TFUG,
>
> I asked this question some time ago, but I still haven't found a
> reliable and/or proper solution.  The problem is how to get a shell
> script to exit when a user logs out of (or exits) X.

So, I've not tested this at all, but a flash of inspiration struck:
When you launch X, most systems will have an ~/.xsession script that
is running as long as your session is active.  Kill ~/.xsession, and
you kill the whole login.  This script is typically just a bash
script.  On my system, ~/.xsession calls ~/.xinitrc, which is the file
that actually calls the window manager.

So, could one not trap a TERM signal to this script, and execute code
when that is heard?

...
exitx() {
  your_code_here
}

trap exitx INT TERM QUIT

gnome-session   # Or your WM of choice
<EOF>



Similarly, the .xinitrc script will run your window manager, but not
launch it into the background; .xsession / .xinitrc will stay running
until the WM exits, then presumably anything after that in the bash
script is executed.  So, you can put the code you want to run on exit
after the WM is launched.  To cover both options (a TERM and a natural
exit), you could possibly do this:

...
exitx() {
  your_code_here
}

trap exitx INT TERM QUIT

gnome-session   # Or your WM of choice

exitx

<EOF>


Again, this is completely untested; I'm at work and can't look into it
now, but it might be an avenue worth pursuing.

Regards,
  Jeff




More information about the tfug mailing list