Search code examples
shellprocessfish

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


I'm spawning a fish shell as a background job and from it I'm spawning inotifywait (a process that watches a specified file and returns only once it has been changed).

Later on, however, at an unspecified point in time (at which point it's not guaranteed that inotifywait had returned), I'm killing the fish shell that spawned inotifywait.

My goal is for inotifywait to terminate automatically as soon as the PPID that spawned it terminates. Is there any way to do this?

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

The temporary hack I'm using right now is to simply update the access time of the file being watched (with a utility such as touch) so that inotifywait would finally return.

I'm using fish 3.0.1 on linux 4.20.11.


Solution

  • Don't use fish for any situation where normal job control semantics are expected. Fish is the best interactive shell I've ever used. It's been my primary login shell for several years. But it's job control has been broken all the way back to the first version. For fun do this:

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

    You won't see the "WTF" message and the backgrounded fish shell won't terminate. Now ps waux | grep sleep and kill the sleep process. You should see the "WTF" message on your terminal and your interactive shell will report the background fish has terminated.

    For this type of situation the simplest solution is to just use bash/ksh/sh for the backgrounded script.