Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

systemd: SIGTERM immediately after start

Tags:

linux

systemd

I am trying systemd for the first time. I want to start a process at system bootup. And I have a problem in getting it up and running.

systemd should run a script (start.sh). This script starts a processes (lets call it P) in the background and exits with code 0. P keeps running forever till a signal happends.

If I run start.sh manually all is ok.

If I let it start by systemd P gets immediately after the start a SIGTERM and terminates.

So it get started but what about the signal?? It terminates P and I am not sure whats its origin and the reason for it.

Maybe my unit is wrong but I have no idea how to set it for my needs. I tried service-type simple, idle and oneshot.

Thanks for help! Chris

Here is my unit.

[Unit]
Description=Test
After=sshd.service

[Service]
Type=oneshot
ExecStart=/home/max/start.sh start
Restart=no
User=root
SuccessExitStatus=0

[Install]
WantedBy=multi-user.target

Thats the status.

Loaded: loaded (/etc/systemd/system/test.service; enabled)
Active: inactive (dead) since Die 2016-02-23 20:56:59 CET; 20min ago
Process: 1046 ExecStart=/home/max/test.sh start (code=exited, status=0/SUCCESS)
like image 976
chris01 Avatar asked Feb 23 '16 20:02

chris01


2 Answers

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

like image 121
mnagel Avatar answered Oct 01 '22 21:10

mnagel


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
like image 45
Alexis Wilke Avatar answered Oct 01 '22 22:10

Alexis Wilke