Ich habe eine C++-basierte Anwendung, die ich als Daemon mit systemd ausführe (ausführbar).
Unit-Datei:
[Unit] Description=Console Service After=network.target [Service] Environment="USER=ubuntu" "Path=/home/ubuntu/console/bin" WorkingDirectory=/home/ubuntu/console/bin ExecStart=/bin/sh -ec "exec /sbin/start-stop-daemon -S -c ${USER} -d ${Path} --pidfile=/var/run/console.pid --oknodo --exec consoleExecutable " #2>/dev/null ExecStop=/bin/sh -ec "exec /sbin/start-stop-daemon -K --quiet -c ${USER} -d ${Path} --pidfile=/var/run/console.pid --retry=TERM/30/KILL/5 --oknodo --exec consoleExecutable" #2>/dev/null Restart=on-failure RemainAfterExit=no TimeoutStopSec=10 SuccessExitStatus=0 1 TimeoutStartSec=360 [Install] WantedBy=multi-user.target
Wenn ich den Startbefehl gebe, startet der Dienst, aber dann erhält er sofort ein Signal zum Herunterfahren und wird dann beendet.
Irgendeine Ahnung, was passiert?
sudo systemctl status console.service ● console.service - Console Service Loaded: loaded (/etc/systemd/system/console.service; enabled; vendor preset: enabled) Active: deactivating (stop-sigterm) since Mon 2017-09-25 19:58:58 UTC; 1s ago Process: 8706 ExecStop=/bin/sh -ec exec /sbin/start-stop-daemon -K --quiet -c ${USER} -d ${Path} --pidfile=/var/run/console.pid --retry=TERM/30/KILL/5 --oknodo --exec consoleExecutable #2>/dev/null (code=exited, status=0/SUCCESS) Process: 8701 ExecStart=/bin/sh -ec exec /sbin/start-stop-daemon -S -c ${USER} -d ${Path} --pidfile=/var/run/console.pid --oknodo --exec consoleExecutable #2>/dev/null (code=exited, status=0/SUCCESS) Main PID: 8701 (code=exited, status=0/SUCCESS) Tasks: 1 Memory: 1.8M CPU: 53ms CGroup: /system.slice/console.service └─8705 consoleExecutable Sep 25 19:58:58 mgmt1 systemd[1]: Started Console Service. sudo systemctl status console.service ● console.service - Console Service Loaded: loaded (/etc/systemd/system/console.service; enabled; vendor preset: enabled) Active: inactive (dead) since Mon 2017-09-25 19:59:01 UTC; 947ms ago Process: 8706 ExecStop=/bin/sh -ec exec /sbin/start-stop-daemon -K --quiet -c ${USER} -d ${Path} --pidfile=/var/run/console.pid --retry=TERM/30/KILL/5 --oknodo --exec consoleExecutable #2>/dev/null (code=exited, status=0/SUCCESS) Process: 8701 ExecStart=/bin/sh -ec exec /sbin/start-stop-daemon -S -c ${USER} -d ${Path} --pidfile=/var/run/console.pid --oknodo --exec consoleExecutable #2>/dev/null (code=exited, status=0/SUCCESS) Main PID: 8701 (code=exited, status=0/SUCCESS) Sep 25 19:58:58 mgmt1 systemd[1]: Started Console Service.
Akzeptierte Antwort:
Environment="USER=ubuntu" "Path=/home/ubuntu/console/bin" WorkingDirectory=/home/ubuntu/console/bin ExecStart=/bin/sh -ec "exec /sbin/start-stop-daemon -S -c ${USER} -d ${Path} --pidfile=/var/run/console.pid --oknodo --exec consoleExecutable " #2>/dev/null ExecStop=/bin/sh -ec "exec /sbin/start-stop-daemon -K --quiet -c ${USER} -d ${Path} --pidfile=/var/run/console.pid --retry=TERM/30/KILL/5 --oknodo --exec consoleExecutable" #2>/dev/null
Das ist dem systemd House of Horror fast würdig. Wäre da nicht schon eine Horrorgeschichte drin, die das tut.
Verwenden Sie nicht start-stop-daemon
in einer Serviceeinheit, um all die Dinge zu tun, die eine Serviceeinheit bereits tut . Mit unnötigen PID-Dateien und der falschen Annahme, dass ExecStart
akzeptiert Shell-Syntax-Kommentare, nicht weniger.
Und tun Sie nicht, was die andere Antwort sagt, und versuchen Sie, sie mit Type=forking
zu umgehen . Das macht die Dinge schlimmer, nicht besser.
Der Unsinn mit start-stop-daemon
deshalb laufen die Dinge schief. Da der Prozess start-stop-daemon
ausführt wird nicht werden der Dienst beendet wird, aber tatsächlich ziemlich sofort beendet wird, denkt systemd, dass Ihr Dienst beendet wird. In Ihrem ersten systemctl status
Ausgabe können Sie sehen, dass systemd gerade dabei ist, SIGTERM
zu senden um alle verbleibenden laufenden Prozesse nach dem Ausführen von ExecStop
zu bereinigen Aktion, was es tut, wenn es denkt, dass ein Dienst beendet wurde.
Machen Sie die Dinge einfach:
Type=simple WorkingDirectory=/home/ubuntu/console/bin User=ubuntu ExecStart=/home/ubuntu/console/bin/consoleExecutable
Kein ExecStop
noch Environment
ist eigentlich erforderlich.
Weiterführende Literatur
- Jonathan de Boyne Pollard (2015). Du brauchst wirklich nicht zu dämonisieren. Wirklich. . Das systemd House of Horror.
- Jonathan de Boyne Pollard (2016). Wenn Sie zwei Dienste haben, definieren Sie zwei Dienste. . Das systemd House of Horror.
- Jonathan de Boyne Pollard (2015). Bereitschaftsprotokollprobleme mit Unix-Dämonen . Häufig gegebene Antworten.
- Systemd beendet den Dienst sofort nach dem Start