6

I am trying to figure out why a specific service is started on a system, and I wonder if there is a command to tell why a specific service was started?

When asking for status on the service, systemctl claims that it is disabled, yet it is running and I haven't explicitly asked to start it, AFAICT. Output below (slightly anonymized)

# systemctl status myservice
● myservice.service - My Service
   Loaded: loaded (/usr/lib/systemd/system/myservice.service; disabled; vendor preset: disabled)
   Active: active (running) since mån 2017-02-27 13:57:15 CET; 30min ago
     Docs: http://www.example.com/
 Main PID: 4680 (ewe)
   CGroup: /system.slice/myservice.service
           ├─4680 /opt/myservice/vbc/bin/myservice
           └─4944 /opt/myservice/vbc/bin/myservice

I am starting a service that has set Before=myservice.service in its .service file (no Requires), so I am suspecting that, but I can't tell for sure.

This is running on a CentOS 7.3 system.

EDITED: I have been able to work around the issue by making sure that a service that the above service has a Requires=, After= and Wants= relationship to, and which is started by a transient run-once service, is explicitly stopped. When doing this, the service is not started. I am not closer to figuring out why it was started in the first place, however.

EDITED: It seems that my service file is started whenever one of the services it has a Requires relationship to is restarted. I did not expect that to happen, I assumed that it would only mean that my service would start the other service when started, not that it would also start mine. Removing Requires fixes the phantom restarts.

nafmo
  • 450
  • 4
  • 13
  • `Before` shouldn't cause `myservice.service` to run if it wouldn't run anyway. It just means "If we are starting Y, start X before it". http://serverfault.com/questions/812584/in-systemd-whats-the-difference-between-after-and-requires/812589#812589 – Sven Feb 27 '17 at 13:49
  • Exactly, that is why I am a bit baffled as to why the service is starting. It shouldn't be. – nafmo Feb 27 '17 at 13:49

2 Answers2

4

Running sudo systemctl status might help, as might systemctl list-dependencies.

I had heard of services being started on install on (I think) Ubuntu, but never on CentOS. However, IF your service has a socket unit defined, then traffic to a given socket/port could be causing your service to start automatically.

I have such a setup for TFTP, for example:

sudo systemctl list-dependencies | grep socket

│ ├─sockets.target
│ │ ├─dbus.socket
<snip>
│ │ └─tftp.socket

Config for it says to start the service if UDP/69 traffic is received:

# /usr/lib/systemd/system/tftp.socket
[Unit]
Description=Tftp Server Activation Socket

[Socket]
ListenDatagram=69

[Install]
WantedBy=sockets.target
iwaseatenbyagrue
  • 3,688
  • 15
  • 24
1

Another reason why a service could be started, which would not be reflected in systemctl list-dependencies, is if it is a D-Bus service which got started on-demand. For instance, looking at the UPower daemon on my system, we can see it is running, yet is not enabled:

systemctl status upower
  ● upower.service - Daemon for power management
       Loaded: loaded (/usr/lib/systemd/system/upower.service; disabled; preset: disabled)
       Active: active (running) since Fri 2023-04-07 10:56:33 EDT; 12h ago

Moreover, we can see that the systemd service exposes a D-Bus interface using the BusName option:

$ systemctl cat upower
# /usr/lib/systemd/system/upower.service
[Unit]
Description=Daemon for power management
Documentation=man:upowerd(8)
  
[Service]
Type=dbus
BusName=org.freedesktop.UPower
ExecStart=/usr/lib/upowerd
Restart=on-failure

Finally—and most importantly—we can see in the system log that it is dbus-daemon which starts our service.

Apr 07 10:56:33 comet-observatory-laptop dbus-daemon[319]:
[system] Activating via systemd: service name='org.freedesktop.UPower' unit='upower.service' requested by ':1.22' (uid=1000 pid=490 comm="workrave")
Koopa
  • 123
  • 2