How to execute a script when xfce session ends
Asked Answered
K

4

6

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

Kulseth answered 28/9, 2011 at 7:2 Comment(2)
you can try this: #5033854Imprimatur
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 programKulseth
B
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.)

Boyette answered 6/10, 2011 at 20:36 Comment(3)
I accepted the answer since it is a good idea, however I would have prefered a solution which does not impact distribution binary files.Kulseth
See 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 palatableMonograph
Disadvantage: overwrite your new batch file each time an update for package xfce4-settings is installed.Dithionite
M
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.)

Monograph answered 12/8, 2013 at 12:48 Comment(2)
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 ~/.logoutEponymous
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)Eponymous
B
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.)

Boyette answered 6/10, 2011 at 20:36 Comment(3)
I accepted the answer since it is a good idea, however I would have prefered a solution which does not impact distribution binary files.Kulseth
See 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 palatableMonograph
Disadvantage: overwrite your new batch file each time an update for package xfce4-settings is installed.Dithionite
G
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

Glennisglennon answered 1/5, 2019 at 20:8 Comment(0)
K
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.

Kulseth answered 20/8, 2012 at 13:44 Comment(2)
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?Monograph
It can do what it wants when the real xfce-session ends : your script must launch it and then block until it terminatesKulseth

© 2022 - 2024 — McMap. All rights reserved.