在 bash 中为 PID 已知的不同进程设置陷阱
我需要为在后台启动的 bash 进程设置一个陷阱。后台进程可能运行很长时间,并将其 PID 保存在特定文件中。
现在我需要为该进程设置一个陷阱,因此如果它终止,PID 文件将被删除。
我有办法做到这一点吗?
编辑#1
看起来我对问题的描述不够精确。我可以完全控制所有代码,但我拥有的长时间运行的后台进程是这样的:
cat /dev/random >> myfile&
当我现在在该语句所在的脚本开头添加陷阱时,$$
将是 PID我从这里开始的是那个更大的脚本而不是这个小的后台进程。
那么如何专门为该后台进程设置陷阱呢?
I need to set a trap for a bash process I'm starting in the background. The background process may run very long and has its PID saved in a specific file.
Now I need to set a trap for that process, so if it terminates, the PID file will be deleted.
Is there a way I can do that?
EDIT #1
It looks like I was not precise enough with my description of the problem. I have full control over all the code, but the long running background process I have is this:
cat /dev/random >> myfile&
When I now add the trap at the beginning of the script this statement is in, $$
will be the PID of that bigger script not of this small background process I am starting here.
So how can I set traps for that background process specifically?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将其添加到 Bash 脚本的开头。
Add this to the beginning of your Bash script.
您可以在显式子 shell 中运行长时间运行的后台进程,如 Petesh 的答案所示,并在此特定子 shell 内设置陷阱来处理长时间运行的后台进程的退出。父 shell 不受此子 shell 陷阱的影响。
You can run your long running background process in an explicit subshell, as already shown by Petesh's answer, and set a trap inside this specific subshell to handle the exiting of your long running background process. The parent shell remains unaffected by this subshell trap.
您不需要
trap
在后台进程终止后运行某些命令,您可以通过 shell 命令行运行并在后台进程后面添加以下命令,用分号分隔(并让此 shell在后台运行而不是后台进程)。如果您仍然希望在 shell 脚本中发送一些通知并捕获 SIGUSR2,例如:
You do not need
trap
to just run some command after a background process terminates, you can instead run through a shell command line and add the command following after the background process, separated with semicolon (and let this shell run in the background instead of the background process).If you still would like to have some notification in your shell script send and
trap SIGUSR2
for instance: