Search code examples
ubuntuprometheussystemdprometheus-node-exporter

Systemd target file starts eventhough the After= service is not started


Okay, first of all, I want to accomplish that a systemd service does not get garbage collected, if it is stopped and not enabled by default. I need this because of a current problem with the node exporter from Prometheus for monitoring purposes.

I did already accomplish my goal but I am not sure why it does work and if there are possible side effects.

So as an example I installed ntp on my Ubuntu 18.03, disabled the service and stopped it. Now I wanted my node exporter to show me, that ntp.service is inactive, but ntp get's garbage collected by systemd. I read that this can be prevented by using, in this case, ntp as dependency somewhere. Using Wants= does not help, since this would result in a restart of my ntp, if my dependent service restarts. So I experimented a bit and I created a test.target file like this:

#/etc/systemd/system/test.target
[Unit]
Description=Testing purpose
After=ntp.service

[Install]
WantedBy=Multi-user.target

I enabled the service and started it with ntp disabled and stopped. Now that is my actual Question. My test.target file does get enabled and starts without a problem, even though ntp.service is not running. Interestingly it also does what I want, ntp does not get garbage collected, even though it is disabled and stopped.

So I really would like to know why this works, why does the After= get's ignored?


Solution

  • The After= is not being ignored, it is causing systemd to load the unit (if it isn't already loaded) and also preventing the garbage collection. The After= doesn't mean it will start ntp.service, for that you should use Requires=.

    Check Unit Garbage Collection, Requires, and After.