2

I have a systemd service which starts multiple 3rd-party processes which output to standard output. Is there anyway to easily determine which logs are from specific process from the system journal?


Diving into systemd recently, I had a question about logging. man systemd-journald.service notes that:

It creates and maintains structured, indexed journals based on logging information that is received from the kernel, from user processes via the libc syslog(3) call, from standard input and standard error of system services or via its native API.

I created a simple service:

[Service]
ExecStart=/home/myself/logtest/runlogtest

And have the script invoke some processes:

#!/bin/bash

...
/home/myself/logtest/app1 &
/home/myself/logtest/app2 &
...

When the service is running, I listen for messages using the following command:

journalctl -f | grep runlogtest

The output is seen as the following (for this example, app1 output 'A' and app2 output 'B'):

Aug 07 14:22:04 localhost.localdomain runlogtest[8742]: B
Aug 07 14:22:07 localhost.localdomain runlogtest[8742]: A
Aug 07 14:22:07 localhost.localdomain runlogtest[8742]: B
Aug 07 14:22:10 localhost.localdomain runlogtest[8742]: B
Aug 07 14:22:11 localhost.localdomain runlogtest[8742]: A
Aug 07 14:22:13 localhost.localdomain runlogtest[8742]: B
Aug 07 14:22:15 localhost.localdomain runlogtest[8742]: A

Is there anyway to know that 'A' logs are coming from app1 (and 'B' messages are from app2)?

The only solutions I can see right now is that I would:

  • Manage each application in its own service (something I'd like to avoid).
  • Make a wrapper for process which I capture their standard output and pump to the journal via syslog.
  • Have you considered adding ps aux | grep 'bash.*<script name>' | tee -a <your log file> to your apps? This will add the pid to the log file for identification. Similarly, you can echo the output before each app output to CMD. – Simply_Me Aug 7 '14 at 19:44
  • I should add that this will also add the process name to the log file; hence it'll give you direct relation between process log and process/app. – Simply_Me Aug 7 '14 at 19:58
4

Make a wrapper for process which I capture their standard output and pump to the journal via syslog.

Such wrapper already exists and is called systemd-cat.

You may use it as follows:

systemd-cat -t app1 /home/myself/logtest/app1 &
systemd-cat -t app2 /home/myself/logtest/app2 &

The argument to -t is an arbitrary identification string, analogous (equivalent) to syslog's identifier.

It is also possible to use systemd-cat in a shell pipeline like this:

/home/myself/logtest/app1 |& systemd-cat -t app1 &

(The |& is a bash construction to pipeline both stdout and stderr.)

However, the first form is preferable as it avoids spawning an extra process and doing an extra copy of all logged data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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