How are "Applications" run with krunner managed wrt processes?

I have a bash script, called runthunderbird, that… runs thunderbird :

sleep 5
thunderbird & disown

When I execute runthunderbird from krunner, Thunderbird starts after 5 seconds as intended, then the runthunderbird process ends, as evidenced by ps -ef or the process list in the System monitor from Plasma 5.27.10.

However, the same System monitor has an Applications tab where runthunderbird remains listed, instead of thunderbird-bin, with the resource (eg. memory) consumption of Thunderbird.

If instead of just Thunderbird, the scripts runs several graphical applications before ending, they are lumped together in the Application tab, again under the name of the (terminated) script.

I am wondering how this is managed and how I should write my script to completely detach thunderbird from the script than runs it.

I am also wondering if this fact is related to Thunderbird not being effectively run when the runthunderbird script is run at login from a desktop file in ~/.config/autostart.

Asked By: ysalmon


It’s managed via cgroups and systemd. (That is, I’m 95% sure it’s managed via cgroups and systemd, although I don’t have KDE installed here to confirm.)

Recently, both GNOME and KDE have begun using systemd’s support for on-the-fly creation of .services and .scopes for launching desktop apps if the user-level systemd --user service manager is running. This is based on the Linux cgroup feature – each systemd .service or .scope corresponds to a Linux cgroup. (Cgroups predate systemd but saw little use before it.)

As soon as the app process has been spawned, they ask systemd to move it into a temporary .scope unit (and accordingly a new cgroup). Later, the task manager can look at the cgroup name of each process via /proc/<PID>/cgroup and read the resource values from /sys/fs/cgroup (which also provides a list of PIDs within each cgroup).

Take a look systemd-cgls or loginctl user-status to see the resulting cgroup tree; check out systemd-cgtop as an example of per-cgroup resource metering. You can even add the "Systemd unit" column in htop or ps, e.g. ps -e -o pid,cmd,unit,uunit.

You might also see individual cgroups being created for each terminal tab; GNOME terminals (libvte) and tmux do this now and I’ve heard that Konsole does as well. (Among other things, it allows each tab to have its own task limit; I’ve successfully ran the "bash smiley" forkbomb in GNOME Terminal and the rest of the system just kept chugging along.)

I am wondering how this is managed and how I should write my script to completely detach thunderbird from the script than runs it.

Cgroups are created a) by starting services, b) on-the-fly with systemd-run --user. To "detach" Thunderbird, you could use:

systemd-run --user [--scope] --collect /bin/thunderbird

(…or you could create a real "thunderbird.service" in your ~/.config/systemd/user/ and run it using systemctl --user start. That, combined with "thunderbird.timer" to delay the startup, would remove the need for your script entirely.)

Use --scope and -u/--unit="app-foo-bar" to make it more similar to what GNOME and KDE are doing. Systemd-run’s default is to create a .service; the difference is that .service units always have their process spawned by systemd itself while .scope allows the caller to move an already running process.

This also means that in scope mode the command will still run "in foreground" unless you & it as usual, whereas in service mode it will always run "in background" and you would need to explicitly ask for -t/--pty if you want to run an interactive terminal app, or -P/--pipe if the program isn’t necessarily interactive but you still want to see its output; otherwise all stdout goes to the system logs (journalctl).

The -G/--collect just prevents stale units from lingering around.

Systemd-run can be useful to specify a cgroup-level memory limit for a potentially large task without risk of OOM’ing the entire system – take a look at the -p/--property parameters.

Answered By: u1686_grawity
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.