linux - bash 中的 fork 和 exec

标签 linux bash scripting shell

如何在 bash 中实现 fork 和 exec?

让我们假设脚本为

echo "Script starts"

function_to_fork(){
sleep 5
echo "Hello"
}

echo "Script ends"

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

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

最佳答案

像在 shell 中一样使用 & 符号。

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

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

关于linux - bash 中的 fork 和 exec,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3096561/

相关文章:

bash - ClearCase:它是错误还是功能?

bash - 使用扩展测试时,循环的正则表达式条件不起作用

Linux Shell 脚本 : backup and restore files while maintaining directory location

python - 在自己的 Ubuntu 服务器上部署 Flask 应用程序错误

linux - 将 Yesod 部署到 Heroku,无法静态构建

linux - 运行 `perf stat` 时 `time` (和 `perf trace -s`!)是否显示不正确的结果?

linux - 如何编写在程序内部运行的命令脚本

javascript - 使用 jQuery,是否可以让它检测某个链接类并打开它们?

python - 使用 Python 脚本控制虚拟机

linux - 如何在 zfs 文件系统中设置默认用户配额