简体   繁体   English

xfce会话结束时如何执行脚本

[英]How to execute a script when xfce session ends

xfce会话停止时是否可以运行脚本/命令?

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 ). 请参阅http://mail.xfce.org/pipermail/xfce/2012-November/031694.html-在那里,Erik Habicht建议在/usr/local/bin/xfce4-session创建包装脚本(或在安装xfce4-session目录,在PATH /usr/bin This way, you do not have to change /usr/bin/X11/xfce4-session , so it can be updated independently. 这样,您不必更改/usr/bin/X11/xfce4-session ,因此可以独立更新。

#!/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. 它不是完美的(取决于PATH顺序),但可能更可口。

( Note: I promoted my comment to an answer. ) 注意:我将我的评论提升为答案。

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. 使用运行原始xfce4-session的外壳程序脚本和如果xfce4-session完成的注销脚本更改/usr/bin/xfce4-session可执行文件。

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

The new /usr/bin/xfce4-session file: 新的/usr/bin/xfce4-session文件:

#!/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.) (在Debian Squeeze上测试。)

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 但是,我找到了另一种处理方式:创建一个X11程序,该程序将在会话启动时启动:它可以在X会话关闭时执行自定义脚本。

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. 注意:缺点是使用的脚本无法连接到X窗口,因此,根据需要,此解决方案可能执行脚本的时间过晚。

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: 创建具有以下内容的~/.local/bin/xfce4-session-logout脚本:

#!/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 现在,将您需要在注销操作时执行的所有内容放入~/scripts/pre-logout.sh并使其可执行

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 重新登录后, menu > logout buttonAlt+f3: "logout"将显示用于退出当前会话的简单对话框

Note: pressing Alt+F4 does not work with it, but maybe some black belted xfce4 users will provide some suggestion 注意:按Alt+F4不能使用它,但是也许有些黑带xfce4用户会提供一些建议

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM