如何在停止当前 运行 bash 文件后执行任意命令

How to execute arbitrary command after Stopping the Current running bash file

我有这个 bash 文件,我曾经 运行 我的 angular 应用程序:

#!/usr/bin/env bash

yarn start

我运行通过 IntelliJ IDEA "Run/Debug Configurations" 菜单将此文件作为可运行目标。

问题是:当我要求 IDEA 停止 bash 运行 时,它停止了它但没有终止节点进程,我端口仍然打开。

我的解决方法是将 bash 文件编辑为:

#!/usr/bin/env bash

echo "cleaning from previous run...."
ps -W | awk '/node.exe/,NF=1' | xargs kill -f
echo "done cleaning"

yarn start

P.S。我在 Windows.

中使用 Cygwin

使用特殊的退出陷阱:

trap 'clean_exit' EXIT

clean_exit() {
    ps -W | awk '/node.exe/,NF=1' | xargs kill -f
}

陷阱在退出时注册 clean_exit