6

Is it possible to run a script/command when the xfce session stops ?

Denis R.
  • 818
  • 1
  • 9
  • 15
  • you can try this: http://stackoverflow.com/questions/5033354/run-script-before-bash-exits – maialithar Sep 28 '11 at 07:44
  • I saw this question too, but it would call the "endscript" for any script that I would launch. In fact, if xfce-session were a script, I could do that but it is a compiled program – Denis R. Sep 28 '11 at 08:49

4 Answers4

5

See http://mail.xfce.org/pipermail/xfce/2012-November/031694.html - There, Erik Habicht suggested creating a wrapper script in /usr/local/bin/xfce4-session (or another dir that precedes the dir where xfce4-session is installed, /usr/bin in your PATH). This way, you do not have to change /usr/bin/X11/xfce4-session, so it can be updated independently.

#!/bin/bash
# Add your own pre-session logic here
/usr/bin/xfce4-session
# Add your own logout logic here

then

$ chmod +x /usr/local/bin/xfce4-session

It's not perfect (depends on PATH order) but may be more palatable.

(Note: I promoted my comment to an answer.)

djb
  • 4,930
  • 1
  • 34
  • 37
  • Finally a solution for xfce logout-hooks, thanks alot! ... maybe again add the hint "chmod a+x /usr/local/bin/xfce4-session" .. missing it caused me some trouble. I will open a xfce bug/feature request to automatically trigger a simple ~/.logout – Alex Aug 07 '15 at 12:48
  • 2
    One remaining problem: It looks like the solution only works if explicitly a "logout" is done. If I directly shutdown my system, the .logout script is not executed. (Distro: Debian Jessie) – Alex Aug 10 '15 at 06:57
2

Change the /usr/bin/xfce4-session executable with a shell script which runs the original xfce4-session and your logout script if xfce4-session finished.

# mv /usr/bin/xfce4-session /usr/bin/xfce4-session.orig

The new /usr/bin/xfce4-session file:

#!/bin/bash
/usr/bin/xfce4-session.orig
echo "my logout script" > /tmp/testfile

Don't forget to set the execute permissions:

# chmod a+x /usr/bin/xfce4-session

(Tested on Debian Squeeze.)

palacsint
  • 28,416
  • 10
  • 82
  • 109
  • 1
    I accepted the answer since it is a good idea, however I would have prefered a solution which does not impact distribution binary files. – Denis R. Aug 20 '12 at 13:47
  • 4
    See http://mail.xfce.org/pipermail/xfce/2012-November/031694.html - that is you can put the wrapper script in /usr/local/bin or another dir that precedes the dir where xfce4-session is installed, and not change /usr/bin/X11/xfce4-session. It's not perfect (depends on PATH order) but may be more palatable – djb Jul 26 '13 at 19:15
  • 1
    Disadvantage: overwrite your new batch file each time an update for package `xfce4-settings` is installed. – kdg1955 May 30 '17 at 10:51
1

I'd prefer solution that does not touch system directories or files and will run the logout hook within current user session and its privilledges.

below is my solution:

create ~/.local/bin/xfce4-session-logout script with the following contents:

#!/bin/bash
PRELOGOUT=${HOME}/scripts/pre-logout.sh

RESULT=RES_`echo -e "logout\nrestart\nshutdown\nsuspend" | zenity --height=250 --list --title "Logout from $USER" --column "What do You want to do?"`
case $RESULT in
    RES_logout)
        [ -x  $PRELOGOUT] && $PRELOGOUT
        /usr/bin/xfce4-session-logout --fast --logout
        ;;
    RES_restart)
        [ -x  $PRELOGOUT] && $PRELOGOUT
        /usr/bin/xfce4-session-logout --fast --reboot
        ;;
    RES_shutdown)
        [ -x  $PRELOGOUT] && $PRELOGOUT
        /usr/bin/xfce4-session-logout --fast --halt
        ;;
    RES_suspend)
        /usr/bin/xfce4-session-logout --suspend
        ;;
    *)
       exit 1
       ;;
esac

and make it executable:

chmod u+x ~/.local/bin/xfce4-session-logout

Now, put whatever You need to be executed at logout action to ~/scripts/pre-logout.sh and make it executable

chmod u+x ~/scripts/pre-logout.sh

after relogin, either menu > logout button or Alt+f3: "logout" will bring simple dialog for leaving the current session

Note: pressing Alt+F4 does not work with it, but maybe some black belted xfce4 users will provide some suggestion

vibrys
  • 21
  • 4
0

I validated the answer above since it does not involve new code writing. I however found another way to proceed : create a X11 program which would be launched at session startup : it could execute custom scripts when the X session is closed

Note : the drawback is that the used scripts could not connect to X windows so this solution may, depending on the need, execute the script too late.

Denis R.
  • 818
  • 1
  • 9
  • 15
  • how does a startup script handle the case of session ending? i.e. does it have to trap a signal, respond to an X event? – djb Aug 11 '15 at 17:28
  • It can do what it wants when the real xfce-session ends : your script must launch it and then block until it terminates – Denis R. Aug 12 '15 at 07:08