如何在shell脚本中启动程序并使shell脚本继续运行,即使程序保持打开状态

vkc1a9a2  于 6个月前  发布在  Shell
关注(0)|答案(5)|浏览(104)

我在Ubuntu上使用bash。我希望有一个shell脚本打开一个程序,并继续到shell脚本的下一行,即使程序没有终止。

z4iuyo4d

z4iuyo4d1#

&添加到命令中会将其置于后台。
举例说明:

/path/to/foo    
/path/to/bar     # not executed untill foo is done

/path/to/foo &    # in background
/path/to/bar &    # executes as soon as foo is started

字符串
阅读更多关于作业控制here和这里

sauutmhj

sauutmhj2#

使用类似(my-long-running-process &)的代码。这将在后台启动脚本作为一个单独的进程。

krcsximq

krcsximq3#

您必须在后台运行该进程,但必须首先启用作业控制。否则,您无法在需要时终止或将该进程置于前台。
要启用作业控制,请执行:

set -m

字符串
要在后台运行某个任务,请执行:

task &


要操作后台任务,请使用 jobspec 语法(%[n])。例如,要终止最后启动的进程,请执行:

kill %


请注意,只有当您实际运行 * 脚本 *(如问题中所述)时才需要启用作业控制。如果以交互方式运行,则作业控制已默认启用。
bash的手册页在JOB CONTROL部分中有更多的信息。

kt06eoxx

kt06eoxx4#

http://ubuntuforums.org/showthread.php?t=1657602
看起来你所要做的就是在行尾添加一个&。

2w2cym1i

2w2cym1i5#

如果您已经知道&将命令置于后台,但仍然查看此处,则可能是因为您的终端在脚本执行后不会再次打印其提示符。或者更确切地说,因为它再次打印其提示符,但您启动的应用程序在终端中输出了一些内容。例如,此脚本

#!/usr/bin/bash
firefox --new-instance -P Something &

字符串
或者这一个,就像@alecov建议的那样,

#!/usr/bin/bash
set -m
firefox --new-instance -P Something &


将在gnome-terminal(Ubuntu 23.10)中给予以下内容:

laurent@laurent-GL73-8SD:~/redacted$ ./fire.sh 
laurent@laurent-GL73-8SD:~/redacted$ Gtk-Message: 22:35:26.454: Not loading module "atk-bridge": The functionality is provided by GTK natively. Please try to not load it.

(firefox:47696): Gtk-WARNING **: 22:35:26.571: GTK+ module /snap/firefox/3289/gnome-platform/usr/lib/gtk-2.0/modules/libcanberra-gtk-module.so cannot be loaded.
GTK+ 2.x symbols detected. Using GTK+ 2.x and GTK+ 3 in the same process is not supported.
Gtk-Message: 22:35:26.571: Failed to load module "canberra-gtk-module"

(firefox:47696): Gtk-WARNING **: 22:35:26.573: GTK+ module /snap/firefox/3289/gnome-platform/usr/lib/gtk-2.0/modules/libcanberra-gtk-module.so cannot be loaded.
GTK+ 2.x symbols detected. Using GTK+ 2.x and GTK+ 3 in the same process is not supported.
Gtk-Message: 22:35:26.573: Failed to load module "canberra-gtk-module"
ATTENTION: default value of option mesa_glthread overridden by environment.
ATTENTION: default value of option mesa_glthread overridden by environment.


如果你想要一个干净的提示符,你需要再次输入回车。

相关问题