7

I have a process which is started by systemd - lets call it A. This process spawns numerous child processes - lets just take one and call it B.

This is a C++ application. When you print to std::cout the output is captured by systemd and can be viewed with the journalctl command.

Whenever a message is printed to std::cout from process A it appears in the journalctl output with the name of process A preceding the log message - makes sense.

Nov 09 16:27:17 hostname processA [1417]: message from process A

Whenever a message is printed from process B, however, the message that is printed is still preceded by the name of process A.

Nov 09 16:27:18 hostname processA [1417]: message from process B

I presume this is expected behaviour as it displaying the name of the process that was actually started by systemd - disregarding the fact it was raised by a child of that process. It does appear as if systemd is aware of there being multiple processes when you use the systemctl status processA command though:

Active: active (running) since Wed 2016-11-09 16:27:20 GMT; 30min ago
 Main PID: 1417 (processA)
   CGroup: /system.slice/processA.service
           ├─1417 /opt/test/bin/processA
           ├─1450 /opt/test/bin/processB

My question is: Is there a way for the output in journalctl to display the child process name when it's output is captured?

6

I've found the answer. You need to use sd_journal_send() from systemd/sd-journal.h. You can also use the SYSLOG_IDENTIFIER and SYSLOG_PID tags to customise what is used. More info on the available tags can be found here.

Example:

std::string sysLogIdentifier("SYSLOG_IDENTIFIER=");
sysLogIdentifier += program_invocation_short_name;

std::string sysLogPid("SYSLOG_PID=");
sysLogPid += getpid();

sd_journal_send("MESSAGE=Found the answer",
                sysLogIdentifier.c_str(),
                sysLogPid.c_str(),
                NULL);

Output:

Feb 10 17:11:48 hostname processB [1418]: Found the answer

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.