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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6948565/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:50:16  来源:igfitidea点击:

How do I launch a program inside a shell script and have the shell script continue, even though the program remains open

bashshell

提问by Jake

I am using bash on Ubuntu. I would like to have a shell script open a program and continue on to the next line of the shell script, even though the program has not terminated.

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

回答by Fredrik Pihl

Adding an &to a command places it in background.

&命令添加会将其置于后台。

example:

例子:

/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

Read more about job-control hereand here

在此处此处阅读有关作业控制的更多信息

回答by arunkumar

Use something like this (my-long-running-process &). This will launch your script as a separate process in the background.

使用这样的东西(my-long-running-process &)。这将在后台将您的脚本作为单独的进程启动。

回答by Corey Ogburn

http://ubuntuforums.org/showthread.php?t=1657602

http://ubuntuforums.org/showthread.php?t=1657602

It looks like all you have to do is add a & at the end of the line.

看起来你所要做的就是在行尾添加一个 & 。

回答by alecov

You must run the process in the background, but you must enable job-control first. Otherwise, you cannot kill or bring the process to foreground if desired.

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

To enable job-control, execute:

要启用作业控制,请执行:

set -m

To run some task in the background, execute:

要在后台运行某些任务,请执行:

task &

To manipulate the background task, use the jobspecsyntax (%[n]). For example, to kill the last launched process, execute:

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

kill %

Note that enabling job-control is required only if you're actually running a script(as stated in the question). If running interactively, job-control is already enabled by default.

请注意,仅当您实际运行脚本时才需要启用作业控制(如问题中所述)。如果以交互方式运行,则默认情况下已启用作业控制。

The manpage for bashhas much more information in the JOB CONTROLsection.

的联机帮助页bashJOB CONTROL部分有更多信息。