如何用&(用bash)制作进程并杀死它们?

Geo*_*zos 7 bash process

我有一个脚本,在后台启动另一个脚本,然后终止它.我期待儿童脚本消失,但最终仍然设法打印一些输出.这是一个例子:

在脚本one.sh:

echo "this is one"
./two.sh &
sleep 1
pid=$!
kill $pid
echo "this was one"
Run Code Online (Sandbox Code Playgroud)

在脚本two.sh:

echo "this is two"
./three.sh
echo "this was two"
Run Code Online (Sandbox Code Playgroud)

在脚本three.sh:

echo "this is three"
sleep 5
echo "this was three"
Run Code Online (Sandbox Code Playgroud)

我跑了./one.sh,它应该在后台运行two.sh,然后运行three.sh但不在后台运行!得到的输出是:

this is one
this is two
this is three
this was one
this was three
Run Code Online (Sandbox Code Playgroud)

不应该在输出中出现"this is three",因为three.sh没有在后台运行而two.sh被one.sh终止了?您是否也可以向我指出任何文档,这些文档描述了进程在后台(而非)在后台运行时的行为以及终止时会发生什么?

非常感谢您的帮助!

exu*_*sum 1

您正在杀死后台进程two.sh,但没有杀死two.sh它及其子进程three.sh

这个问题:

杀死所有子进程的最佳方法

有更多关于终止子进程的信息。