systemd: SIGTERM immediately after start

When start.sh finishes, systemd kills everything in the same cgroup as start.sh

Your options are:

  • setting KillMode in the Unit section to process (the default is control-group). That will cause systemd to only kill the process which it directly fired.

  • to not make start.sh start something in the background and exit but to execute it right there in the foreground

I think in your situation option 2 is viable and more straightforward.

Source: https://unix.stackexchange.com/a/231201/45329


Although changing the KillMode to process like below will work in your situation, it is not the recommended solution.

[Service]
KillMode=process
...

The problem with KillMode set to process is that systemd loses control over all the children of the process it started. That means, if anything happens and one of your processes does not die for some reason, it will continue to linger around.

A better solution in your situation would be to create all the processes, keep their pid and then wait on them.

The wait command that you use in your shell script may vary depending on which shell you are using (the link I proposed is for bash). Having the shell script wait for all the children is in effect the same as starting one child, which does not get detached, in the foreground.

So something like this, more or less:

#!/bin/bash

# Start your various processes
process1 &
PROCESS1_PID=$!
process2 &
PROCESS2_PID=$!
process3 &
PROCESS3_PID=$!

# Wait on your processes
wait $PROCESS1_PID $PROCESS2_PID $PROCESS3_PID

# OR, if I'm correct, bash also allows you to wait on all children
# with just a plain wait like so:
wait

# reach here only after children 1, 2, and 3 died

Tags:

Linux

Systemd