You are not logged in.

#1 2022-04-16 19:29:18

jlindgren
Member
Registered: 2011-02-27
Posts: 260

Prevent processes from escaping systemd-run service

I am looking for a way to start a terminal, wait for the shell process within it to exit, and then also reliably terminate any child processes launched from that shell.

Example:
- Start xfce4-terminal
- Run "geany & exit" from within the terminal
- At this point, I want geany to be terminated

Launching a temporary user service:

systemd-run --user --wait xfce4-terminal

almost works.  However, there is one problem: xfce4-terminal starts the shell in a new systemd scope, which "escapes" from the systemd-run service and thus isn't terminated when the service exits.

The output of "systemctl status --user" looks like this (abbreviated):

   CGroup: /user.slice/user-1000.slice/user@1000.service
           ├─app.slice
           │ ├─run-u40.service
           │ │ └─67756 /usr/bin/xfce4-terminal
           │ └─vte-spawn-8389cbfb-9aa2-47d6-88fb-e98151149163.scope
           │   ├─67780 bash
           │   ├─67782 geany
           │   └─67829 systemctl status --user

By patching xfce4-terminal to use the VTE_SPAWN_NO_SYSTEMD_SCOPE flag [1], I can disable the creation of the extra scope, so that geany is terminated along with the terminal.
In that case, the output of "systemctl status --user" looks like this (abbreviated):

   CGroup: /user.slice/user-1000.slice/user@1000.service
           ├─app.slice
           │ └─run-u35.service
           │   ├─67654 /usr/bin/xfce4-terminal
           │   ├─67678 bash
           │   ├─67679 geany
           │   └─67705 systemctl status --user

Is there any way around this issue (so that processes launched from the shell are terminated) without patching xfce4-terminal?
I guess I'm imagining something like a systemd-run option that restricts the creation of new scopes.

1. https://github.com/jlindgren90/xfce4-te … 78492bca22

Offline

#2 2022-04-17 02:05:21

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 25,285

Re: Prevent processes from escaping systemd-run service

Have you tried the --scope option? It pretty much reads like that's the purpose and intention of it.

I'm getting XY vibes,  what's your end goal here? The way I'm interpreting you are looking for a way to just run a distinct xfce-terminal for the duration of a specific command. Why bother with all of that and not just use xfce-terminal4 -e=geany ?

Last edited by V1del (2022-04-17 02:11:38)

Offline

#3 2022-04-17 13:39:03

jlindgren
Member
Registered: 2011-02-27
Posts: 260

Re: Prevent processes from escaping systemd-run service

The way I'm interpreting you are looking for a way to just run a distinct xfce-terminal for the duration of a specific command.

No, that was just a reduced test-case to demonstrate the xfce4-terminal behavior.  Sorry if that confused you.  I'm actually running an entire user session via systemd-run.  As background, years ago I used to invoke "pkill -u $USER" at the end of the session (after the WM exits) to clean up all the other processes in the session.  Then systemd-run became available, which allows using a cgroup to track and terminate all forked processes when the main process (the WM in this case) exits.  It seemed like a better alternative to using pkill, so I switched over.

Per my post above, it's mostly reliable -- the only problem is when a process is started from xfce4-terminal and ends up in a separate scope.  Then that process "escapes" from the systemd-run service and isn't terminated when the WM exits.  I've worked around this by patching xfce4-terminal, but I'd like to find a more permanent solution.

I tried using the --scope argument to systemd-run per your suggestion, but that made things worse -- none of the other processes in the session are terminated when the WM exits.

Offline

Board footer

Powered by FluxBB