如何在 Linux 中打开一个程序的多个实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7553452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to open multiple instances of a program in Linux
提问by Pavan Manjunath
Say for example, to open multiple instances of gedit
editor I wrote a shell script like this-
例如,要打开多个gedit
编辑器实例,我编写了这样的 shell 脚本-
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
.
但是在我运行 shell script 后./example.sh
,我只能找到一个 gedit 实例!我什至使用了&
运算符,因此外壳不会等待一个实例完成。我仍然看不到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?
我也直接从命令提示符尝试。如果我只是gedit&
在命令行上输入,它会显示1906
(这是新创建gedit
进程的 PID ),启动一个新gedit
实例并再次返回提示。当我gedit&
在命令行上输入时,这次显示了1909
,但没有新的 gedit 实例!而且我PID 1909
在系统监视器中也找不到任何进程。这个新流程去哪儿了?
Is the happening specific to gedit
? If so, what is the generic behavior when creating multiple instances of a program?
发生的特定于gedit
? 如果是这样,创建程序的多个实例时的通用行为是什么?
采纳答案by etuardu
It is specific to gedit. You are likely looking for gedit --new-window &
.
它特定于 gedit。您可能正在寻找gedit --new-window &
.
From man gedit
:
来自man gedit
:
--new-window Create a new toplevel window in an existing instance of gedit.
--new-window Create a new toplevel window in an existing instance of gedit.
回答by jpjacobs
This seems specific to gedit, perhaps there's some option to turn off the check for a running instance.
这似乎特定于 gedit,也许有一些选项可以关闭对正在运行的实例的检查。
回答by Alexander Janssen
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:
看起来 gedit 首先寻找一个正在运行的实例,只是忽略了进一步的启动请求(只是一个疯狂的猜测)。但是手册页说,您可以打开另一个窗口:
--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.
祝你好运,亚历克斯。
回答by mxmlnkn
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:
只允许一个实例实际上更难实现,因为程序需要找到已经运行的实例并与之通信。这是通过 D-Bus 完成的。为了防止与已经启动的实例通信,您可以在另一个 D-Bus 会话中运行该程序:
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.
注意:即使终端关闭,nohup 也会保持程序运行。
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.10.1 and dbus 1.8.16-1
使用 xfce 4.10.1 和 dbus 1.8.16-1 进行测试
For Scite:
对于 Scite:
scite -check.if.already.open=false &
回答by David McCorkle
Using this in a script. I've found that it does what I need it to:
在脚本中使用它。我发现它可以满足我的需求:
#!/bin/bash
xterm -e "gedit; bash" &disown