父鱼 shell 进程终止后立即终止子进程?

Terminate child process as soon as the parent fish shell process terminates?

我正在生成一个 fish shell 作为后台作业,然后从中生成 inotifywait(一个监视指定文件和 return 的进程仅更改一次)。

然而,稍后,在一个未指定的时间点(此时不能保证 inotifywait 已经 returned),我正在杀死 fish shell 产生了 inotifywait

我的目标是 inotifywait 在生成它的 PPID 终止后立即自动终止。有什么办法吗?

fish -c "inotifywait -qq $somefile && ..." &
# time passes, inotifywait hasn't returned
kill -15 (jobs -lp)
# the fish process is now terminated, but inoyifywait remains

我现在使用的临时 hack 是简单地更新正在观看的文件的访问时间(使用 touch 等实用程序),以便 inotifywait 最终 return.

我在 linux 4.20.11 上使用鱼 3.0.1

不要在任何需要正常作业控制语义的情况下使用 fish。 Fish 是我用过的最好的互动 shell。多年来,它一直是我的主要登录 shell。但是它的作业控制一直被打破到第一个版本。为了好玩,这样做:

fish -c 'trap "echo WTF; exit 3" TERM; sleep 666' &
kill (jobs -lp)

您不会看到 "WTF" 消息,后台鱼 shell 也不会终止。现在 ps waux | grep sleep 并终止睡眠进程。您应该会在终端上看到 "WTF" 消息,您的交互式 shell 将报告背景鱼已终止。

对于这种情况,最简单的解决方案是只对后台脚本使用 bash/ksh/sh。