问题
I need to run external program from systemd service outside current cgroup.
I have found a systemd-run
tool which would probably solve this but i'm stuck with systemd version 204 where it is not yet available.
Is there some other solution?
My problem in more detail:
Application is bundled in debian package and will install and run itself as systemd service.
This service later downloads newer version of application and runs dpkg -i myapplication.deb
.
Service must be stopped during installation of upgrade - but it will not stop until all processes in cgroup ends. Now we have deadlock because dpkg
is itself running in this cgroup. After service stopping timeout, dpkg
is killed and upgrade is not installed.
回答1:
You can create your service dynamically in /run/systemd/system path. This path is the runtime service directory.
Later on you tell systemd to reload its state. Once the reload is done, now systemd knows about your service. Starting your service will happen in service's own cgroup.
I think you could do something like this:
#!/bin/sh
printf "[Service]\nType=oneshot\nExecStart=dpkg -i myapplication.deb" \
> /run/systemd/system/my-dpkg.service
if [ -f "/run/systemd/system/my-dpkg.service" ]; then
systemctl daemon-reload && \
systemctl start dpkg.service
fi
来源:https://stackoverflow.com/questions/26674151/how-to-run-external-program-from-systemd-service-in-independent-cgroup