<div dir="ltr"><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Aug 17, 2022 at 5:05 AM Peter Hoeg <<a href="mailto:peter@hoeg.com">peter@hoeg.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><br>
Hi all,<br>
<br>
I have a small program that queries an external web API, massages the data and passes it off to something else. As I only need it to run during certain windows and I didn't want to have to bother with making sure it was alive, I am starting it using a systemd timer which is sufficiently flexible. All good and it's super nice to be able to "outsource" all that to systemd.<br>
<br>
My question is - what is the canonical way to detect that I'm running under systemd so that I can adjust accordingly? Currently I'm checking for the existence of the "_SYSTEMD_INVOCATION_ID" environment variable in order to change the logging function as the default logger includes a timestamp which is redundant when journald is picking it up.<br>
<br>
I'm also using LoadCredential for passing tokens but that just comes down to looking for the right file name where the CREDENTIALS_DIRECTORY variable points, so that really isn't systemd specific and there is a fallback in case that isn't set or the directory/file doesn't exist anyway.<br>
<br>
I could of course also add a --systemd flag that toggles this but if I can do "TheRightThing(tm)" out of the box, why not.<br></blockquote><div><br></div><div>What *is* specific to systemd that you need to check for and how would that change the program's behavior? It sounds to me that being started by pid1 is irrelevant here – if you want to alter your logging, then you want to check specifically whether stderr is tied to the journal, for which you can use JOURNAL_STREAM (like in <a href="https://github.com/systemd/systemd/blob/main/src/basic/log.c#L217">https://github.com/systemd/systemd/blob/main/src/basic/log.c#L217</a>).<br></div><div><br></div><div>(In addition to disabling the timestamps you should *at least* add message priority indicators as well, either by using SyslogLevelPrefix=, or by avoiding stderr entirely and using the syslog() API for logging – if not going all the way with sd_journal_send*().)<br></div></div><br>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr">Mantas Mikulėnas</div></div></div>