How to teach systemd to detect service process is already running

systemd

There is a daemon that can be started from command line and from systemctl.

/etc/systemd/system/mydaemon.service contains ExecStart= and PIDFile= so Systemd can surely detect that service is running or not:

[Unit]
After=network-online.target

[Service]
Type=simple
ExecStart=/usr/local/bin/mydaemon --no-fork
PIDFile=/var/run/mydaemon-%i.pid

[Install]
WantedBy=multi-user.target

Why "systemctl status mydaemon" says that daemon is stopped even if it's running?

How to teach systemd that daemon can be running not only via "systemctl start" but also directly?

Best Answer

Don't do it. Always start it with systemctl. Shutdown the service and bring it up in such a way the systemd knows about it.

This probably isn't the answer you want. It may even be possible to do what you're asking, but why bother?