M HYPE SPLASH
// news

xfce: Prevent multiple instances of apps

By Sarah Scott

What is really annoying in Ubuntu is that when some apps which don't need multiple instances such as 'appearance settings', a web browser, terminal window are loaded again when they are aready launched. For a browser or terminal I can still understand, although such apps have a 'new window' option. But for settings apps, multiple instances of e.g. 'Appearence' or 'Keyboard settings' are completely useless.

How do I prevent that the launcher launches an app (e.g. Firefox) again when it is already launched ?

I tried a shellscript called launch.sh:

#!/bin/sh
if ! pgrep $1 >/dev/null; then "$2" &
fi

In the xfce launcher for Firefox I set

launch.sh firefox /usr/bin/firefox

which does work (firefox is not launched when it is areay open), but for xfce4-appearance-settings it does not work, because pgrep xfce4-appearance-settings returns nothing while xfce4-appearance-settings is opened.

In macOS it works properly: such apps are launched only once and when clicking again on the app icon, it moves to the foreground rather than starting a new obsolete instance.

How can this be realized in Ubuntu ?

3

1 Answer

It is the application itself that controls that behavior.

A command, e.g. for firefox, that switches to a running window, and only launches the program if it is not running, looks like:

wmctrl -x -a firefox || firefox

In bash, the command separator || means "OR". The first command, wmctrl, attempts to switch to a window of WM_CLASS "firefox". If that fails, firefox is executed.

The first instance firefox refers to the WM_CLASS of a firefox window. You can see the WM_CLASS of running windows in the output of wmctrl -lx.

To use this for a shortcut key, or in a .desktop launcher, use following command:

sh -c "wmctrl -x -a firefox || firefox"

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy