I have a custom daemon that is managed by upstart on my Ubuntu server. It works perfectly except that I need to capture (log) the daemon's output. The official stanzas page says that I can use console logged to do this, but what file does it log to?

I've also read that console logged is no longer a valid stanza. I'm currently using 0.3.9 (Hardy) but will upgrade to 0.6.x (Lucid) in a few months. If console logged in fact won't work with later versions, what do I use instead?

link|improve this question
1  
Can you simply update your custom daemon to send the output to syslog, or to a logfile specified in the daemon's configuration file? – Zoredache Feb 18 '10 at 2:22
feedback

3 Answers

This is ugly but so far the best I have found

exec /path/to/server >> /tmp/upstart.log 2>&1

link|improve this answer
feedback

This snippet will pipe the output of your service into logger, while still allowing you to exec the service process (thus replacing the shell process) so that upstart doesn't get confused. It also makes sure the logger process is reparented to init, so it's not a child of your service, and it avoids leaving cruft sitting around in the filesystem, even though it needs to create a fifo temporarily.

script
  set -e
  mkfifo /tmp/myservice-log-fifo
  ( logger -t myservice </tmp/myservice-log-fifo & )
  exec >/tmp/myservice-log-fifo
  rm /tmp/myservice-log-fifo
  exec myservice 2>/dev/null
end script

Here's how it works:

  1. mkfifo /tmp/myservice-log-fifo simply makes the fifo special file (aka named pipe). Type man 7 fifo for more info.
  2. ( logger ... </tmp/myservice-log-fifo & ) starts logger reading from the fifo, in the background. The parens cause the logger process to be reparented to init, rather than remaining a child of the current shell process.
  3. exec >/tmp/myservice-log-fifo redirects the current shell's stdout to the fifo. Now we have an open file descriptor for that fifo, and we don't actually need the filesystem entry any more...
  4. rm /tmp/myservice-log-fifo so we'll remove it.
  5. exec myservice 2>/dev/null simply runs the service in the usual way. Stdout is already going to the fifo, and that won't change when the new program executes.
link|improve this answer
great answer, and doesn't leave fifo files lying around anywhere. – Ash Berlin Mar 29 at 12:25
feedback

You can also redirect the output to syslog, e.g.

exec $SERVER 2>&1 | logger -t myservice -p local0.info

However, the pipeline may cause upstart to confuse the PID of the logging process with the PID of the daemon.

link|improve this answer
The daemon confusing the PID is actually a useless daemon, since it is supposed to watch the process, respawn it then. Any idea on how to make sure the right PID is being watched? – Johann Philipp Strathausen Feb 10 at 11:04
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.