2

I have an init.d script that runs a binary as a daemon. The binary regularly writes status lines to stdout. I would like to log those lines into /var/log/my-daemon.

When the start code is like this:

start-stop-daemon -S -x $DAEMON -- $ARGS

And I run service my-daemon restart, then I get the log output on... stdout. But obviously, I want to run it in background, and save the logs to a file.

I want something like this (inspired by this):

start-stop-daemon -S --background -x $DAEMON -- $ARGS >> /var/log/my-daemon 2>&1

But this does not log anything to the log file. From the man page of start-stop-daemon, it sounds like --no-close would be the right thing to use here. But my system uses start-stop-daemon from Busybox:

BusyBox v1.32.0 () multi-call binary.

Usage: start-stop-daemon [OPTIONS] [-S|-K] ... [-- ARGS...]

Which does not seem to support -C or --no-close.

Is there a solution to that, or do I need to change my init system because I won't be able to achieve what I want with Busybox?

Note: the following seems to work (at least sometimes), but feels wrong (otherwise there would be no need for --background, right?):

start-stop-daemon -S -x $DAEMON -- $ARGS >> /var/log/my-daemon 2>&1 &
JonasVautherin
  • 7,297
  • 6
  • 49
  • 95
  • What about $DAEMON itself. Is that something you have control on? You could make it write to the log file rather than to STDOUT maybe – whites11 Jul 12 '21 at 11:41

1 Answers1

1

Have you considered using syslog(3) in your binary process? This is fairly standard across *NIX distributions, and also fairly easy to use. The point of "backgrounding" the process is (among other things) to detach the process from the console, so it will dead-end stdin, stderr, and stdout -- but if you choose to redirect to a file in your init script, it will of course send the output to the redirected file. By using syslogd to route your messages, you can configure this without modifying your init scripts, or passing special parameters. You will also get the benefit of verbosity levels. If you wish to send output to both a log and the console, you can pretty easily wrap syslog() as well.

cyberbisson
  • 382
  • 2
  • 9