Shortcut to Stop Any Window Managers - Gracefully

     2 minutes to read

$ sudo pkill X

Just kidding, are you sure you want to do that ? that's considered as anti-pattern for me.


One of the most exciting stuff about linux desktop is; You have the most customizable desktop OS in the entire universe because there is a lot of different desktop environment and window manager that you can use right ?
Today you decide to use GNOME, and then next day you're bored and open your highly customized bspwm and so on, It so fun.

Logout Manager

So, imagine you have a multiple setups are ready to use, let says Openbox, i3-wm, and Xfce4.
And then you see some beautiful logout manager like Clearine or Oblogout.
But after you install those program, you also want to use that on every desktop you had.

Bash Script !

Yes, here it is.

We are just need to write a simple bash script that detecting what window manager are currently active!

#!/usr/bin/env bash
# wm-exit.sh

case $active_wm in
"Openbox")
openbox --exit
;;
"i3")
i3-msg exit
;;
"Xfwm4")
xfce4-session-logout -l
;;
esac

For the $active_wm, we can use those things like $XDG_CURRENT_DESKTOP or $DESKTOP_SESSION.
But wait, that's are not fully-universal, it will not works for those who uses no display manager, and different distros may have a different preset of those environment variable like Ubuntu.

So, how can we detect that without having a compatibility issues ?

wmctrl

wmctrl provides an interface to standard window management tasks, it also can read the name of currently active window manager.

Since for now we only care about that, lets just jump into it :

$ wmctrl -m

wmctrl-m

Huuray!
we got the name, so lets put that things into our script before.

#!/usr/bin/env bash
# wm-exit.sh

active_wm=$(wmctrl -m | awk '/Name: / {printf "%s\n", $NF}')
case $active_wm in
"Openbox")
openbox --exit
;;
"i3")
i3-msg exit
;;
"Xfwm4")
xfce4-session-logout -l
;;
esac

set it executable by using chmod :

$ chmod +x wm-exit.sh

And you can call it from wherever you want (e.g. clearine, oblogout, polybar, etc).


bashlinuxwindowmanager
WRITTEN BY

Nanda Okitavera

Latest Content

2018