Linux 在 bash 中 fork 和 exec

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

fork and exec in bash

linuxbashscriptingshell

提问by Abhijeet Rastogi

How do I implement fork and exec in bash?

如何在 bash 中实现 fork 和 exec?

Let us suppose script as

让我们假设脚本为

echo "Script starts"

function_to_fork(){
sleep 5
echo "Hello"
}

echo "Script ends"

Basically I want that function to be called as new process like in C we use fork and exec calls..

基本上我希望该函数被称为新进程,就像在 C 中我们使用 fork 和 exec 调用一样..

From the script it is expected that the parent script will end and then after 5 seconds, "Hello" is printed.

从脚本中,预计父脚本将结束,然后在 5 秒后打印“Hello”。

采纳答案by mob

Use the ampersand just like you would from the shell.

像在 shell 中一样使用和号。

#!/usr/bin/bash
function_to_fork() {
   ...
}

function_to_fork &
# ... execution continues in parent process ...

回答by Jubal

How about:

怎么样:

(sleep 5; echo "Hello World") &