如何监控两个进程&如果退出则执行某些操作(bash 脚本)?
我正在尝试在 bash 中编写一个包装脚本来自动启动和关闭我需要同时运行的三个程序:
#!/bin/bash
gnome-screensaver-command -i -n aeolus-wrapper.sh &
aeolus &
qjackctl
启动它们后,我需要监视 aeolus 和 qjackctl 并在 aeolus 或 qjackctl 退出时终止剩余的两个进程:
# if aeolus exits, kill gnome-screensaver-command and qjackctl
# if qjackctl exits, kill gnome-screensaver-command and aeolus
这这就是我被困住的地方。我对 很感兴趣这个示例展示了如何使用until
循环来监视进程并在进程终止时重新启动它,但我不太确定如何从那里到达我想去的地方。任何建议都非常受欢迎。
I'm trying to write a wrapper script in bash to automate startup and shutdown of three programs I need to run simultaneously:
#!/bin/bash
gnome-screensaver-command -i -n aeolus-wrapper.sh &
aeolus &
qjackctl
After starting them, I need to monitor aeolus and qjackctl and kill the remaining two processes if either aeolus or qjackctl exits:
# if aeolus exits, kill gnome-screensaver-command and qjackctl
# if qjackctl exits, kill gnome-screensaver-command and aeolus
This is where I'm stuck. I was intrigued by this example that shows how to use an until
loop to monitor a process and restart it if it dies, but I'm not quite sure how to get from there to where I want to go. Any suggestions very welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要使用 while 循环。只需阻塞并等待 SIGCHLD 告诉您其中一个进程已终止。在陷阱中,终止剩余的正在运行的进程。例如:
这将运行 3 个命令。当其中一个退出时,另外两个将被发送 SIGTERM。
Do not use a while loop. Just block and wait for a SIGCHLD to tell you that one of the processes has terminated. In the trap, kill the remaining running processes. For example:
This will run 3 commands. When one exits, the other two will be sent SIGTERM.
与
qjackctl
相同的另一种语法,
same for
qjackctl
Another syntax,