Bash 脚本 - 运行应用程序的当前实例或新实例

发布于 2024-08-04 07:16:16 字数 104 浏览 9 评论 0原文

我在 ubuntu 中添加了应用程序的快捷方式。每次我按下快捷键时,都会创建一个新的应用程序实例。我只需要一个实例,我可以使用什么脚本来打开应用程序的现有实例或在应用程序未运行时创建一个新实例?

I added a shortcut to an application in ubuntu. Each time i press shortcut keys, a new instance of application is created. I need only one instance, what script i can use to open the existing instance of application or creates a new one if the application is not running?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天荒地未老 2024-08-11 07:16:16

这很大程度上取决于应用程序。最简单的情况是已知的进程名称只能由您运行(即系统上没有杂散实例):

pidof applicaton || /usr/bin/application

打开现有应用程序可能会很棘手,并且再次取决于应用程序。我建议您看一下 alltray 东西,并特别注意 alltray - -help 输出。

由 ypnos 编辑:
如果您只想将此限制为当前用户,您应该使用 pgrep 而不是 pidof,如下所示:

pgrep -u `id -u` application || application

It depends greatly on the application. The most trivial case would be the known process name which can only be run by you (that is, no stray instances on the system):

pidof applicaton || /usr/bin/application

Opening an existing application may get tricky and, again, depends on the application. I'd suggest that you take a look at alltray thingie and pay special attention to the alltray --help output.

Edit by ypnos:
If you want to restrict this to the current user only, you should use pgrep instead of pidof, like this:

pgrep -u `id -u` application || application
节枝 2024-08-11 07:16:16

您还可以尝试这样的脚本:

#! /bin/bash
user=`id -un`
lock=/tmp/$user-$1-lock
if [ -e $lock ]; then
    exit #app is already running
fi

touch $lock
$@
rm $lock

如果您将脚本称为“runner.sh”,则可以像这样使用它:

runner.sh xcalc

用于 pidof 不起作用的情况。否则就选择黑客的解决方案,它也更强大。

You can also try a script like this:

#! /bin/bash
user=`id -un`
lock=/tmp/$user-$1-lock
if [ -e $lock ]; then
    exit #app is already running
fi

touch $lock
$@
rm $lock

If you called the script 'runner.sh', you can use it like this:

runner.sh xcalc

Is for cases where pidof doesn't work. Else go for hacker's solution, which is also more robust.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文