How to open multiple instances of a program in Linux

15,606

Solution 1

It is specific to gedit. You are likely looking for gedit --new-window &.

From man gedit:

--new-window
       Create a new toplevel window in an existing instance of gedit.

Solution 2

I came here, trying to start multiple instances of audacious.

Allowing only one instance is actually harder to implement, because the program needs to find and communicate with the instance already running. This is done via D-Bus. In order to prevent communication with the already started instance you can run the program in another D-Bus session:

nohup dbus-run-session audacious &
nohup dbus-run-session audacious &

Note: nohup will keep the program running even if the terminal is to be closed.

This method should also work for other programs which do not let the user choose between multiple instance vs. one instance.

Beware that this might introduce bugs, if multiple instances are accessing the same configuration files.

Tested with xfce 4.14.1 and dbus 1.12.20

For Scite:

scite -check.if.already.open=false &

A word of caution:

If you, like me, have your system running for multiple months and have edited some of your shortcuts or aliases to open with this hack, then after a while some programs will not start anymore because there are already too many open D-Bus session. In this case you have to kill the started D-Bus sessions, which do not close when the started program closes. The other way around, killing the D-Bus session, will also kill the opened program, so use with care! For me personally, I have some long running autostarted programs which I want to keep open (firefox), so I kill all but the first 10 D-Bus sessions with this:

for pid in $( ps --sort start_time -aux | grep dbus-daemon | tail +10 | awk '{ print $2; }' ); do kill $pid; done

The cleanest solution would be to write a launcher script which waits for the program to finish and then closes the opened D-Bus sessions. But this is a bit more difficult than it seems because it is hard to find the PID of the corresponding D-Bus session.

P.S.: I also used this hack because there seems to be some program on my system which, after a while, slows down the system's default file open dialog to take multiple minutes if not longer to open! Programs then seem to hang when trying to save or open files. A new D-Bus sessions seems to fix this for some reason. While writing this, I found that pkill gvfsd-trash also works and that it may have been this bug. So until this gets shipped, I guess I'll add pkill gvfsd-trash to my crontab.

Solution 3

This seems specific to gedit, perhaps there's some option to turn off the check for a running instance.

Solution 4

Looks like gedit is first looking for a running instance and simply ignores further start-requests (just a wild guess). But the manual page says, that you can open another window:

--new-window
              Create a new toplevel window in an existing instance of gedit.

That wouldn't exactly solve your problem, but maybe that's what you were looking for in the first place.

Good luck, Alex.

Solution 5

Using this in a script. I've found that it does what I need it to:

#!/bin/bash
xterm -e "gedit; bash" &disown
Share:
15,606
Pavan Manjunath
Author by

Pavan Manjunath

Software Engineer @ Lyft, SF Bay Area

Updated on June 17, 2022

Comments

  • Pavan Manjunath
    Pavan Manjunath almost 2 years

    Say for example, to open multiple instances of gedit editor I wrote a shell script like this-

    gedit&
    gedit&
    gedit&
    gedit&
    

    But after I ran my shell script ./example.sh, I can find only one instance of gedit! I've even used the & operator, so that the shell doesn't wait for one instance to finish. Still I cannot see four instances of gedit.

    Also I tried directly from the command prompt. If I just enter gedit& on the command line, it showed 1906 ( this is the PID of the newly created gedit process ), started one new gedit instance and returned to prompt again. When I typed gedit& on the command line, it showed 1909 this time, but no new instance of gedit! And I couldn't find any process with PID 1909 in the System Monitor too. Where did this new process go away?

    Is the happening specific to gedit? If so, what is the generic behavior when creating multiple instances of a program?

  • TC1
    TC1 over 12 years
    Not just gedit, tons of apps, actually. E.g., most browsers will check for a running instance and only open a new tab if they find an instance. @Pavan Manjunath You gotta check the man for gedit, it most likely has a parameter like --new-instance or something like that that you need to pass to get a new window.
  • jpjacobs
    jpjacobs over 12 years
    that's what I meant, just specific to the app he tried running, not something related to the shell :)
  • Pavan Manjunath
    Pavan Manjunath over 12 years
    But what about the new processes getting created and then vanishing? If shell decides against opening a new instance, then what was the need to create a new process and then kill it?
  • jpjacobs
    jpjacobs over 12 years
    My guess is that the shell opens a process that checks if there's an instance running. If it finds one, whatever data needs to be passed on (like a file to open, ...) gets passed, and the second process exits.
  • pooley1994
    pooley1994 almost 9 years
    I have no idea how this answer was accepted. The OP specifically said he wanted multiple instances, and this answer specifically states that it will open a window in an existing instance...
  • etuardu
    etuardu almost 9 years
    The OP bumped into an X Y problem. I guess the title of this question should have been updated in order to mention gedit rather than talking about a generic "program". It is ideed misleading put this way. However, if you want to run multiple istances of something, reading the OP attempts should be enough enlightening.
  • Homero Esmeraldo
    Homero Esmeraldo over 4 years
    This didn't work for me on Ubuntu 18.04 trying to open texmaker.
  • Homero Esmeraldo
    Homero Esmeraldo over 4 years
    Although, this solution which I thought would work for any program didn't work, If you came here because of Texmaker, here's the solution (specific to texmaker): texmaker -n tex.stackexchange.com/questions/126412/…
  • Williams
    Williams almost 3 years
    Thank you. Legend. This is the answer for my question elsewhere. Great answer. askubuntu.com/questions/1343783/…