1

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.

kolypto
  • 10,738
  • 12
  • 51
  • 66
  • Isn't this what things like `supervisord` are supposed to handle? – Michael Hampton May 28 '14 at 19:08
  • @MichaelHampton, you're right, but in this case I really need to print something out. Supervisor is not good at printing :) – kolypto May 28 '14 at 19:10
  • I read your other question too. You really should describe what's actually going on, and name the applications you're working with. – Michael Hampton May 28 '14 at 19:10
  • Could you try tail -f --pid="$$" filename? – kupson May 28 '14 at 19:17
  • @MichaelHampton, okay, let's say that's Apache. It can only log to files or syslog, and not to stdout. I need to have logging to stdout :) – kolypto May 28 '14 at 19:17
  • I got that part already. But it isn't Apache. What is it really? – Michael Hampton May 28 '14 at 19:20
  • @MichaelHampton, supervisor running multiple processes: apache, redis, cron (bundled them to simplify the setup). In other cases this can be any app which only can write to logfiles. stdout logs is a requirement for 12-factor apps to run in a cloud, namely, CoreOS – kolypto May 28 '14 at 19:31
  • @kupson hahaha oh my gosh, never thought `tail` can do that! Amazing! Post as an answer, I'll accept it :) – kolypto May 28 '14 at 19:33
  • Apache can log to anything you want, including an external command. So using it as an example makes absolutely no sense. – Michael Hampton May 28 '14 at 19:51
  • @MichaelHampton, ErrorLog `|cat` does not output anything since when Apache launches this external command - it discards its output – kolypto May 28 '14 at 19:56

1 Answers1

1

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

kupson
  • 3,388
  • 18
  • 18