Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the way to avoid systemctl from killing one of my child processes on a stop?

I have been trying to do an upgrade from within a server by running the simple Debian upgrade command:

sudo apt-get upgrade

However, if that upgrade includes myself (i.e. say I have a package named server and part of the upgrade includes a newer version of server,) then the upgrade fails. More precisely, whenever dpkg runs the preinst script, the process running the command shown earlier gets killed because it runs systemctl stop server.

The code does something like this:

// start with systemd
systemctl start server

// the server runs another daemon which manages the computer packages
// this is C++ code within server doing something like:
if(fork() == 0)
{
    execv("package_manager", ...);
}

// if I look at the output of systemctl status server, I see:
systemctl status server
  CGroup: server
          +-- package_manager

// then package_manager decides to do an upgrade, it starts a process
system("do_server_upgrade");

// in the do_server_upgrade code, I use fork() again to leave the
// existing set of daemons (server and package_manager)
int main()
{
    if(fork() != 0)
    {
        return 0;
    }
    setsid();
    signal(SIGHUP, SIG_IGN);
    ...run the commands to apply the upgrades...
}

As per this stackoverflow answer, I learned that systemd will kill (send SIGTERM) all the children processes in the same control-group.

So I was thinking that was a neat feature for all the other daemons run by server... except for that one do_server_upgrade. In other words, I would appreciate to keep it that way (i.e. KillMode=control-group) but it does not look like there is a way to get a child process out of that control group.

My question here is: Does someone know about such capability? Can I keep my service set to control-group and kick some processes out of that control group?

like image 220
Alexis Wilke Avatar asked Dec 11 '25 06:12

Alexis Wilke


1 Answers

You can ask systemd to start a process inside a new control group by using the systemd-run command.

like image 185
Timothée Ravier Avatar answered Dec 14 '25 00:12

Timothée Ravier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!