3

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?

Community
  • 1
  • 1
Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
  • No, there's no such capability. – Sam Varshavchik Jul 20 '16 at 02:22
  • @SamVarshavchik, ah well... too bad. My server already tracks what is running and kills everything anyway, but it would have been nice from `systemd` to let a few escape once in a while (instead of all or nothing...) – Alexis Wilke Jul 20 '16 at 03:13
  • 1
    Of course you can get a process out of your control group. Just use a tool like `at` or `cron` to start it, instead of directly starting that process yourself. – MSalters Jul 20 '16 at 08:06

1 Answers1

2

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

Timothée Ravier
  • 498
  • 4
  • 11