Using xlsclients to launch programs once from a window manager config

I spent some time mucking around with the awesome and xmonad tiling window managers this weekend. I ended up with an xmonad config that I’m sure is nothing special but that works well for me. One thing that I do want is to launch certain programs (like nm-applet) when I first log in. On top of that, I want to be able to restart the window manager without exiting all my running programs. So you can’t just unconditionally launch nm-applet from the window manager startup, because then after 5 restarts you’d end up with 6 nm-applets running.

All the guides suggest you deal with this by launching the extra programs you want run from your .xinitrc (which is only ever run once), but AFAICT that file is ignored by modern Linux distributions. Then they’ll often offer some kind of solution that’s a lot of complicated configuration specific to the window manager you’re using. My hack works regardless of window manager, and simply asks the X server if a given application is running yet.

awesome version:awful.util.spawn_with_shell( "xlsclients | grep -q nm-applet || nm-applet" )

xmonad version:spawn "xlsclients | grep -q nm-applet || nm-applet"

This obviously only works if the program you’re starting actually connects to the X server.

About the author

Living the good life in Seattle, occasionally sharing something interesting with the Internet.