this post was submitted on 30 Nov 2026
19 points (95.2% liked)

Linux

68985 readers
788 users here now

From Wikipedia, the free encyclopedia

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).

Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.

Rules

Related Communities

Community icon by Alpár-Etele Méder, licensed under CC BY 3.0

founded 7 years ago
MODERATORS
 

I'm running a script that starts a load of other processes to do its work.

I'm running the script like

systemd-cat -t foo thescript

If two of these script runs happens at once, I can't tell them apart because both will tag with foo and journalctl identifies as foo[123] but 123 seems to be the pid of the process that generated the content. So if 123 is the pid of the thescript process at the start, it changes during the run of that script when a process launched by that script generates output.

I'd like a solution like: the 123 is always the parent script's pid. Or some other way to add field data in that could then be used to separate out the separate runs.

Anyone got any suggestions?

top 5 comments
sorted by: hot top controversial new old
[–] dontbelievethis@sh.itjust.works 9 points 2 days ago (2 children)

The [123] can change because journald attributes each line to the process that wrote it. Rather than fighting that, tag the whole run with something unique.

Simplest: unique identifier per run

systemd-cat -t "foo-$$" thescript

Then journalctl -t foo-<pid> gives you one run. You can match all of them with journalctl -o short | grep 'foo-', but filtering by a fixed field is cleaner (see below).

Better: run it as a transient unit

systemd-run --unit="foo-$(date +%s)" --collect --pipe --wait thescript

Every child process lands in the same cgroup, so every line gets the same _SYSTEMD_UNIT and _SYSTEMD_INVOCATION_ID, regardless of which process wrote it. Then:

journalctl -u 'foo-*'
journalctl _SYSTEMD_INVOCATION_ID=<id>

Add your own field

If you want a custom field to filter on, LogExtraFields= attaches it to everything the unit logs:

systemd-run --unit="foo-$$" -p LogExtraFields=RUN_ID=$$ --collect --pipe --wait thescript
journalctl RUN_ID=12345

Add --user if you're not root and want a user-level unit. systemd-cat itself can't add extra fields, but systemd-run does everything it does here and more.

Does that make sense?

[–] Zelaf@sopuli.xyz 3 points 2 days ago

This guy systemds

[–] nettie@lemmy.world 2 points 2 days ago

This looks super helpful, thanks so much! I'll have a play!

[–] non_burglar@lemmy.world 2 points 2 days ago (1 children)

Why aren't stdout and error out enough to handle these tasks?

[–] nettie@lemmy.world 1 points 2 days ago

True, I could just redirect it all to a file with unique name, then wrote a cron to clean those up over time, instead of using systemd journal.