0

I have a small service written in C on Debian.
This service automatically starts with Linux and does some file operations and other things as expected.
The service prints some informational messages and also error situations to standard output. This is visible in a console window when I run the binary from console not as a service.

The only thing I didn't manage to accomplish so far is write its output into the journal, when the binary is run as a service.

I went through all search hits I found, I am querying the journal as root and I see system messages starting/stopping the service, but nothing that the service binary prints.
I simply print with printf("hey this should appear in the journal"); - but there is nothing to see in the journal.

According to what is explained everywhere, all output of a service should automatically be directed to the journal, right?
What could I be missing so that this doesn't happen?

Edit
I found special functions to call for logging, but actually I like the idea to get standard output automatically either on the console or in the journal, depending on how I run the binary.
I tried a bash script service and found it writes echo messages into the journal as expected.

6
  • "What could I be missing", maybe Send output to systemd-journal from C++ program or How can I send a message to the systemd journal? Commented Oct 6, 2024 at 15:57
  • 1
    "I simply print with printf("..."); - but there is nothing to see in the journal.", I would expect a printk there if I remember it right. Commented Oct 6, 2024 at 15:59
  • @U880D printk is used in the kernel, it’s not available in user land. Commented Oct 6, 2024 at 16:00
  • @StephenKitt, good hint. Commented Oct 6, 2024 at 16:06
  • 1
    You need to ensure your output is flushed (see the update to my answer). Commented Oct 6, 2024 at 17:35

1 Answer 1

3

When run using systemd, a unit’s standard output goes to whatever the unit’s StandardOutput= configuration setting specifies. That defaults to the system configuration’s DefaultStandardOutput= value, and that defaults to journal. So if your system uses default settings, units’ standard output should go to the journal.

For any given unit, you can check that setting by running

systemctl show -P StandardOutput yourunit

(replacing yourunit as appropriate).

One thing to check is whether your program is flushing its output: with printf, you need to include a newline (\n) or explicitly call fflush.

As a general rule, log messages should be written using syslog, not sent to standard output; see Proper way to write C code that injects message into /var/log/messages? for details.

2
  • I tried your answer in the link, using syslog works fine. I had newlines after my messages, so this should have worked too. StandardOutput goes to journal, as show tells me. I also tried fflush but without success. My code can determine how to log, so it's no big effort to change that. But nevertheless I am really curious why the default behavior doesn't work for me. Commented Oct 6, 2024 at 17:51
  • It is weird, especially since a shell script has its output in the journal... Commented Oct 6, 2024 at 19:14

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.