How to run external program from systemd service in independent cgroup

泄露秘密 提交于 2020-01-06 06:52:36

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!