Single-Instance Chromium App Shortcuts in Linux

Update (Sep 2022): As this issue mostly affects Chrome Shortcuts/Apps, the Clutter Free Chrome extension is a significanlty easier way to prevent duplicate app instances.

After recently installing Linux Mint 19.3 Cinnamon I started my usual creation of Chromium Application Shortcuts to the dozen or so webapps I use frequently. Gmail, YouTube, Google Calendar, Reddit, my local unRAID servers etc. I like having these sites as separate windows and icons in the task switcher (and in Plank).

Task Switcher

One problem I ran into is everytime I re-launched a Chromium shortcut it opened a new window. Sometimes I wouldn’t even notice until later using the App switcher and suddenly see 3 Gmail icons.

I really hoped that there would be a chromium command line switch that would tell the app to re-use an existing window, but I was unable to find one. Instead I ended up modifying the .desktop launcher file for each Chromium shortcut to run a small script that checks if the instance is running already and use that, otherwise launch a new window.

Here is the chromium_app_singleton.sh script to see if a Chromium instance is already running. It will get executed via the .desktop launcher file with the unique app_id as the first argument.

1
2
3
4
5
6
7
8
9
10
11
#! /bin/bash
app_id=$1
wm_class_id="crx_${app_id}.Chromium-browser"

if wmctrl -xl | grep "${wm_class_id}" > /dev/null ; then
# Running. Bring to front.
wmctrl -x -R "${wm_class_id}"
else
# Not Running. Open new window.
/usr/bin/chromium-browser --profile-directory=Default --app-id=${app_id}
fi

I put the above file into /usr/local/bin/chromium_app_singleton.sh

Next we need to modify the .desktop launcher files to run this script instead of just launching a new Chromium window each time.

Change to the ~/.local/share/applications directory. Each of the chrome-*-Default.desktop files are a launcher for one of your Chromium shortcuts. Within each of these files is a line that looks like:

1
Exec=/usr/bin/chromium-browser --profile-directory=Default --app-id=[UNIQUE_APP_ID]

This needs to be changed in each file to instead read:

1
Exec=/usr/local/bin/chromium_app_singleton.sh [UNIQUE_APP_ID]

If you only have one or two to change, you may as well do it manually. If, like me, you instead have a dozen or more to change, here is a short sed command to quickly rewrite the necessary line in all the files in one go.

1
sed -i 's:/usr/bin/chromium-browser --profile-directory=Default --app-id=:/usr/local/bin/chromium_app_singleton.sh :' ~/.local/share/applications/chrome*.desktop

Now when you go to launch one of your Chromium Application Shortcuts, you should find you’ll only ever have a single instance of each shortcut type running at one time.