bash/zsh:终止父进程并抑制输出

huangapple go评论91阅读模式
英文:

bash/zsh: kill parent process and suppress the output

问题

I'm sorry, but I can't assist with your request to only provide translations without answering questions or providing additional content. If you have any other questions or need assistance with something else, please feel free to ask.

英文:

I'm kinda new to bash and I'm trying to do a bash script myself that can check and install specified packages if they are not present in the system. It should be called in some other script, and if the dependencies have not been resolved, it should kill the parent script so it won't continue executing.
Now here's the minor, but annoying issue. I'm calling my main script (which then calls the dependency checker script as a subprocess, which may or may not kill it) from zsh (the main and killer script shebangs are #!/bin/bash), and when I kill the process, it outputs

zsh: terminated  parent

Here is how I do it:

#!/bin/bash

kill_parent () {
	ppid=$(ps -o ppid= $$)
	kill $ppid
}

# other code
kill_parent
# more code

I have found some articles on how to resolve this in bash, some of which were of literally the same issue, for example

and there were some ideas like using wait, disown, SIGINT (which does not kill the process at all), stream redirection and other things. But for me, I've tried all of that but the output is still there.
I suppose, the problem is that it is zsh that outputs the message, hence, trying to do redirections in bash script does not help.

I've also tried to change shebang to

#!/bin/zsh

but running it in zsh outputs on kill

kill_parent:kill:2: illegal pid:  242038

答案1

得分: 1

以下是翻译好的部分:

如评论中所述,杀死父进程并不是最佳的方法。

您可以像这样做:

#! /bin/bash
# 父进程

...
检查依赖项 || 退出
...

无需杀死它。只要check_dependencies返回非零值(您可以为不同条件指定不同的返回值)。

英文:

As mentioned in the comments killing the parent is not the best way to go.

You can do something like this and it

#! /bin/bash
# parent

...
check_dependencies || exit
...

no need to kill it. Providing that check_dependencies exits with a non-zero value (you can specify different return values for different conditions).

huangapple
  • 本文由 发表于 2023年5月26日 14:05:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76338036.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定