Limit a gui program in Linux to only one instance

I would recommend replacing the link to the program with a link to a shell script that checks if the program is running, and if it is, it uses the window manager's function to bring the program foremost, and if it isn't, starts it.


I found this thread and implemented it, sharing my version.

I created a executable file /usr/local/bin/run_once.sh containing

#! /bin/bash
application=$1
if wmctrl -xl | grep "${application}" > /dev/null ; then
    # Already running, raising to front
    wmctrl -x -R "$application"
else
    # Not running: starting
    $@
fi

This checks using wmctrl if the application you are trying to start already has a window open (some gui programs keep workers without a gui running) instead of using ps, using -x to use the WM_CLASS instead of the title-bar name.

For each program that I only want one window of I copied the system .desktop file to ~/.local/share/applications and changed the exec field from exec=program --arguments to exec=/usr/local/bin/run_once.sh program --arguments


Generally the application source code must be modified. It is not something done by the operating system.