<div dir="ltr"><div>Hi!</div><div><br></div>So I'm testing a program repeatedly and using `systemd-run` to start a service with it, passing it a specific unit name.<div><br></div><div>When the test finishes and I bring down the service, I want to be able to collect the journald logs for that execution of the test alone.</div><div><br></div><div>Right now what I'm doing is naming the service differently every time, including a random number, so I can collect the logs for that service alone at the end. Such as:</div><div><br></div><div>  # myservice_name=myservice-${RANDOM}.service</div><div>  # systemd-run --unit="${myservice_name}" --remain-after-exit mybin --myarg1 --myarg2 ...</div><div><br></div><div>And then collecting the logs using:</div><div><br></div><div>  # journalctl -u "${myservice_name}"</div><div><br></div><div>One disadvantage of this approach is that the units pile up as I keep running tests...</div><div><br></div><div>  # systemctl status myservice-*.service</div><div><br></div><div>And that it's hard to find which one is the latest one, from an unrelated session (this works only while active):</div><div><br></div><div>  # systemctl list-units --state running myservice-*.service</div><div><br></div><div>I would like to run these tests all under a single unit name, myservice.service. I'm fine with not having more than one of them at the same time (in fact, that's a feature.)</div><div><br></div><div>But I wonder how I can get the logs for a single execution...</div><div><br></div><div>The best I could come up with was using a cursor to get the logs for the last execution:</div><div><br></div><div>  # journalctl -u myservice MESSAGE_ID=39f53479d3a045ac8e11786248231fbf --show-cursor<br></div><div>  -- Logs begin at Thu 2018-03-22 22:57:32 UTC, end at Fri 2018-03-23 19:17:01 UTC. --</div><div>  Mar 23 16:40:00 mymachine systemd[1]: Started mybin --myarg1 --myarg2</div><div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration-style:initial;text-decoration-color:initial">  Mar 23 16:45:00 mymachine systemd[1]: Started mybin --myarg1 --myarg2b</div><span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">  Mar 23 16:50:00 mymachine systemd[1]: Started mybin --myarg1 --myarg2 --myarg3</span><br class="gmail-Apple-interchange-newline"></div><div>  -- cursor: s=abcde12345...;i=123f45;b=12345abcd...;m=f123f123;t=123456...;x=...</div><div><br></div><div>And then use the cursor to query journald and get the logs from the last execution:</div><div><br></div><div>  # journalctl -u myservice --after-cursor '<span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">s=abcde12345...;i=123f45;...'</span></div><div><br></div><div>That works to query the last execution of the service, but not a random one...</div><div><br></div><div>I guess what I'm looking for is a way to get systemd to inject a journal field to every message logged by my unit. Something like an environment variable perhaps? Or some other field I can pass to systemd-run using -p. Or something that systemd itself generates, that's unique for each execution of the service and that I can query somehow (perhaps `systemd show` while the service is up.) Is there any such thing?</div><div><br></div><div>Any other suggestions of how I should accomplish something like this?</div><div><br></div><div>Thanks!</div><div>Filipe</div><div><br></div></div>