Kill child process when the parent exits

I'm preparing a script for Docker, which allows only one top-level process, which should receive the signals so we can stop it.

Therefore, I'm having a script like this: one application writes to syslog (bash script in this sample), and the other one just prints it.

#! /usr/bin/env bash
set -eu

tail -f /var/log/syslog &

exec bash -c 'while true ; do logger aaaaaaaaaaaaaaaaaaa ; sleep 1 ; done'

Almost solved: when the top-level process bash gets SIGTERM -- it exists, but tail -f continues to run.

How do I instruct tail -f to exit when the parent process exits? E.g. it should also get the signal.

Note: Can't use bash traps since exec on the last line replaces the process completely.


If you are lucky^Wusing tail command from GNU coreutils you can use --pid=<number> option. The capital -F option would make you safe against log rotation.

tail --pid="$$" -F /var/log/syslog &

More info: http://www.gnu.org/software/coreutils/manual/html_node/tail-invocation.html