1

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?

5
  • Are you sure your daemon writes the corresponding PID file when it starts? Commented Jul 6, 2017 at 15:55
  • Yes, I'm sure. PID file is actual. Commented Jul 7, 2017 at 12:22
  • Could you share your .service file? Commented Jul 9, 2017 at 20:22
  • Well, mydaemon.service file added. Commented Jul 11, 2017 at 1:20
  • The PIDFile is so the service know the PID that it started, not so that it will poll it. Add a script that exits if it's already running. Commented Aug 4, 2020 at 23:28

3 Answers 3

5

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?

2
  • 1
    Example use case: embedded system needs to start job before full OS is loaded to satisfy startup time constraint--yes, loading systemd and dependencies would take too long. It would be nice if systemd could pick up the pre-existing process to manage it from then on. Commented May 10, 2024 at 2:29
  • Example use case: oracle dbas like to start and stop databases using sqlplus. If they shut down and restart Oracle this way, systemd no longer knows about the running database and it can't shut it down. So yes, "don't do it" is good advice, but reality dictates the need for the OP's idea anyway. I indeed wish there were a systemd adopt foo.service option. :) Commented Jun 12, 2024 at 15:27
0

Well, I checked systemd manual and haven't found any information how to inform systemd about manually started process. I don't look deep, but i think there is could be possibility, to do it trough sending messages to systemd about process start, but it's to complicated for me.

0

systemd can only track the process that it has started. If another instance of the same daemon is started via cmdline the systemd is not aware of it.

Now even if you invoke the service file through systemctl , the ExecStart will fail ( since you already launched the daemon via cmdline ) and systemd thinks that it didn't succeed in launching back your daemon.

To resolve this, first kill your daemon. Then invoke your service file via systemctl to restart your daemon, the status should be active

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.