6

There are some gui programs where if you select them from the menu, if it is already open, instead of opening a new instance, that window will become foremost. However, there are some programs that will open multiple instances of themselves.

There is a certain program that exhibits the latter behavior but I want it to do the former. How can this be done?

8

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.

2
  • When I do ps ax | grep for the program it shows its status as S when it's running. If I close the program, then it doesn't disappear from the list. Instead it shows its status as S+. What does that mean? – tony_sid Aug 2 '10 at 14:35
  • 3
    ^^^ That's a new question. ;-) – Chris Nava Aug 2 '10 at 14:37
6

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

2

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

3
  • Unfortunately this is incorrect as the other answers have shown. – Asfand Qazi Jun 25 '20 at 9:07
  • The other answers to date are but workarounds. Nothing wrong with that approach except it is not robust or self-contained. You might want to learn about the contents of /var/run and/or /run. – fpmurphy Jun 25 '20 at 13:09
  • Nonsense. They are perfectly sound. You can look for a GUI's title or Window Class and go from there. – RichieHH Aug 14 '20 at 8:04

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.