gpt4 book ai didi

bash - 下一个命令完成后,如何停止从 bash 启动的后台进程?

转载 作者:行者123 更新时间:2023-11-29 09:35:52 24 4
gpt4 key购买 nike

如果我关闭 Firefox 窗口或以某种方式停止此脚本,我如何管理此处称为后台进程的 morbo-server 将自动关闭/终止?

#!/bin/bash

morbo Mojolicious_Lite.pl &

firefox -new-window http://localhost:3000/

最佳答案

好的,让我们解决这个问题。

#!/bin/bash
morbo Mojolicious_Lite.pl & P=$!
trap "kill $P" INT # maybe you want EXIT here too?
firefox -new-window http://localhost:3000/
wait

这个应该可以工作...当 firefox 退出时,shell 将等待剩余的作业(morbo),然后可以通过 Ctrl-C 中断 - 在这种情况下,陷阱会杀死它们。

您可以使用

进行视觉测试(即查看执行的内容)
bash -x run.sh

假设您的脚本名为 run.sh ;)

关于bash - 下一个命令完成后,如何停止从 bash 启动的后台进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6779494/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com