systemd keeps restarting my daemon

I’m planning to use raspberry as USB tunnel through WiFi/Ethernet, so I wrote scripts to automate binding in udev, compiled and inserted kernel modules, and now I’m stuck with the daemon.
My previous approach was working fine on old system, where daemon was started in rc.local.

What I added in /etc/systemd/system/usbipd.service:

[Unit]
Description=usbip host daemon
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
Restart=always
ExecStart=/usr/sbin/usbipd -D

[Install]
WantedBy=multi-user.target

What I got in syslog:

Sep  9 23:47:25 raspi-conn systemd[1]: Started usbip host daemon.
...
Sep  9 23:47:26 raspi-conn systemd[1]: usbipd.service: Succeeded.
Sep  9 23:47:27 raspi-conn systemd[1]: usbipd.service: Scheduled restart job, restart counter is at 1.
...
Sep  9 23:47:27 raspi-conn systemd[1]: Stopped usbip host daemon.
Sep  9 23:47:27 raspi-conn systemd[1]: Started usbip host daemon.
...
Sep  9 23:47:27 raspi-conn usbipd: usbipd: info: starting usbipd (usbip-utils 2.0)
Sep  9 23:47:27 raspi-conn usbipd: usbipd: info: listening on 0.0.0.0:3240
Sep  9 23:47:27 raspi-conn usbipd: usbipd: info: listening on :::3240
Sep  9 23:47:27 raspi-conn usbipd: usbipd: info: shutting down usbipd
Sep  9 23:47:27 raspi-conn systemd[1]: usbipd.service: Succeeded.
...
Sep  9 23:47:27 raspi-conn systemd[1]: usbipd.service: Scheduled restart job, restart counter is at 2.
...
Sep  9 23:47:27 raspi-conn systemd[1]: Stopped usbip host daemon.
...
Sep  9 23:47:27 raspi-conn systemd[1]: Started usbip host daemon.
...
Sep  9 23:47:28 raspi-conn usbipd: usbipd: info: starting usbipd (usbip-utils 2.0)
Sep  9 23:47:28 raspi-conn usbipd: usbipd: info: listening on 0.0.0.0:3240
Sep  9 23:47:28 raspi-conn usbipd: usbipd: info: listening on :::3240
...
Sep 10 08:15:17 raspi-conn systemd[1]: usbipd.service: State 'stop-sigterm' timed out. Killing.
Sep 10 08:15:17 raspi-conn systemd[1]: usbipd.service: Killing process 560 (usbipd) with signal SIGKILL.
Sep 10 08:15:17 raspi-conn systemd[1]: usbipd.service: Failed with result 'timeout'.
Sep 10 08:15:17 raspi-conn systemd[1]: usbipd.service: Scheduled restart job, restart counter is at 3.
Sep 10 08:15:17 raspi-conn systemd[1]: Stopped usbip host daemon.
Sep 10 08:15:17 raspi-conn systemd[1]: Started usbip host daemon.
Sep 10 08:15:17 raspi-conn usbipd: usbipd: info: starting usbipd (usbip-utils 2.0)
Sep 10 08:15:17 raspi-conn usbipd: usbipd: info: listening on 0.0.0.0:3240
Sep 10 08:15:17 raspi-conn usbipd: usbipd: info: listening on :::3240
...
Sep 10 08:16:47 raspi-conn systemd[1]: usbipd.service: State 'stop-sigterm' timed out. Killing.
Sep 10 08:16:47 raspi-conn systemd[1]: usbipd.service: Killing process 1449 (usbipd) with signal SIGKILL.
Sep 10 08:16:47 raspi-conn systemd[1]: usbipd.service: Failed with result 'timeout'.
Sep 10 08:16:47 raspi-conn systemd[1]: usbipd.service: Scheduled restart job, restart counter is at 4.
Sep 10 08:16:47 raspi-conn systemd[1]: Stopped usbip host daemon.
Sep 10 08:16:47 raspi-conn systemd[1]: Started usbip host daemon.
Sep 10 08:16:47 raspi-conn usbipd: usbipd: info: starting usbipd (usbip-utils 2.0)
Sep 10 08:16:47 raspi-conn usbipd: usbipd: info: listening on 0.0.0.0:3240
Sep 10 08:16:47 raspi-conn usbipd: usbipd: info: listening on :::3240
...
Sep 10 08:18:18 raspi-conn systemd[1]: usbipd.service: State 'stop-sigterm' timed out. Killing.
Sep 10 08:18:18 raspi-conn systemd[1]: usbipd.service: Killing process 1482 (usbipd) with signal SIGKILL.
Sep 10 08:18:18 raspi-conn systemd[1]: usbipd.service: Failed with result 'timeout'.
Sep 10 08:18:18 raspi-conn systemd[1]: usbipd.service: Scheduled restart job, restart counter is at 5.
Sep 10 08:18:18 raspi-conn systemd[1]: Stopped usbip host daemon.
Sep 10 08:18:18 raspi-conn systemd[1]: Started usbip host daemon.
Sep 10 08:18:18 raspi-conn usbipd: usbipd: info: starting usbipd (usbip-utils 2.0)
Sep 10 08:18:18 raspi-conn usbipd: usbipd: info: listening on 0.0.0.0:3240
Sep 10 08:18:18 raspi-conn usbipd: usbipd: info: listening on :::3240

I didn’t have too much experience with systemd internals and I don’t know how to debug it. There’s no clear reason why process is being stopped and then restarted, it looks like systemd doesn’t get process state.

Your Type= and ExecStart= definitions don’t match.

You’ve set Type=Simple, which expects a program that does not fork into the background, yet you use the -D argument which forks the process into the background.

Either omit the -D argument or use Type=forking.

I would prefer the first option, as it allows systemd to handle the logging.

Answered By: Gerald Schneider
Categories: Answers Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.