6

I used Upstart's provided example for console output.

/etc/init/test.conf

console output

pre-start script

  # Perform whatever checks you like here (maybe checking
  # '/etc/default/foo' to see if the service is enabled # or not).
  #
  # if there are no problems detected, simply "exit 0", else do
  # something like this...

  # display an error message to stderr *on the console* and also write
  # the same message to the system log.
  logger -is -t "$UPSTART_JOB" "ERROR: foo!"

  # tell Upstart not to start the main process for the job.
  exit 1
end script

# this service doesn't do much :-)
exec sleep 999

And then as root

$ initctl start test
initctl: Job failed to start

The message is present in /var/log/syslog, but the message was not present in the console.

Jul 23 07:42:19 paul test[26595]: ERROR: foo!

How can I log errors to the console?

This is Ubuntu 14.04,

$ initctl version
init (upstart 1.12.1)
Paul Draper
  • 78,542
  • 46
  • 206
  • 285

1 Answers1

3

console output writes to /dev/console, which, by default, is the system virtual logging device: it's not going to write to your personal console (e.g. /dev/tty0).

You can actually test the difference in behavior if you attempt to write directly to these virtual devices:

sudo echo test >> /dev/console     # Won't work
sudo su; echo test >> /dev/console # Works but nothing prints
sudo echo test >> /dev/tty0        # Works and prints

For more on the differences between the different virtual console devices, see this Unix StackExchange question.

If you really want, you can change what /dev/console points to by modifying your boot configuration:

# /etc/default/grub

# If you change this file, run 'update-grub' afterwards to update
# /boot/grub/grub.cfg.

GRUB_CMDLINE_LINUX="console=tty0 console=ttyS0,115200n8"

...but if I were you, I'd just use the console log stanza instead of console output and then tail the generated file.

Community
  • 1
  • 1
fny
  • 31,255
  • 16
  • 96
  • 127
  • So when the docs say `display an error message to stderr *on the console*`, they mean `display an error message to stderr *on the sys admin's console*`? I'm not sure what the utility of that would be. – Paul Draper Aug 08 '16 at 14:28
  • It's going to be to whatever console to which you've set `/dev/console`, which may or may not be the sys admin's console, but that's a very reasonable default: there are security implications to logging system processes in the open. – fny Aug 08 '16 at 15:51