Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash Shell脚本在退出时终止后台进程_Bash_Kill_Background Process - Fatal编程技术网

Bash Shell脚本在退出时终止后台进程

Bash Shell脚本在退出时终止后台进程,bash,kill,background-process,Bash,Kill,Background Process,我有这样一个脚本: #!/bin/sh exec ./cmd1& exec ./cmd2 如果我仅终止脚本cmd2被终止,cmd1将继续运行。 这两个进程是否都会自动退出?不要执行第二个exec(第一个也是冗余的),而是让shell等待它。杀死外壳可能会杀死命令;如果没有,则: trap 'kill -15 $kids; exit 143' TERM cmd1 & kids=$! cmd2 & kids="$kids $!"

我有这样一个脚本:

#!/bin/sh
exec ./cmd1&
exec ./cmd2
如果我仅终止脚本
cmd2
被终止,
cmd1
将继续运行。
这两个进程是否都会自动退出?

不要执行第二个
exec
(第一个也是冗余的),而是让shell等待它。杀死外壳可能会杀死命令;如果没有,则:

    trap 'kill -15 $kids; exit 143' TERM
    cmd1 &
    kids=$!
    cmd2 &
    kids="$kids $!"
    wait

这对我来说不起作用,但是谢谢你的回答(我在OSX上,这里有些不同…)在OSX上测试-你是如何杀死的?我想在一个应用程序包中运行这个。当我点击它时,dock图标会一直跳转,我所能做的就是强制退出它。我相信退出会更好。你不需要为循环设置一个
,你可以将多个PID传递到
杀死