Is a service enabled on your systemd enabled system?

Today I noticed hundreds of logs in the snapwatchdog services. These appear because the daemon checks whether clamav-freshclam is enabled. This is a daemon used to make sure fresh virus signatures are uploaded at least once a day.

Aug 23 18:14:42 hostname snapwatchdogserver[10305]: Failed to get unit file state for clamav-freshclam.service: No such file or directory

The snapwatchdog service runs its tests about once a minute. This means we check whether the clamav-freshclam service is enabled once a minute. That's 1,440 times a day, assuming we don't lose even one minute. That's quite a bit of log for nothing.

So I looked closer into systemd to find a way to replace the is-enabled test with something that would not generate an error. There is a solution with the show command instead.

Old way:

systemctl is-enabled clamav-freshclam

New way:

systemctl show -p UnitFileState clamav-freshclam

The first way generates all of those errors whenever the service is not found. The second way just outputs the following when the service is not installed:

UnitFileState=

For us, the only difference with the is-enabled command is the output. In one case, we get an error and in the other we get a variable no matter what and no error. It's easy enough to know whether it is enabled from that show command. We do the following test against the command's output:

return output == "UnitFileState=enabled";

If the service is enabled, then that return statement returns true to the caller.

In my case, I further check whether the service is active with:

systemctl is-active clamav-freshclam

Or course, I run that second command only if the first one returned with true. In other words, if the service is not enabled (or not even installed) there is really no reasons to run the is-active command.

We use Ubuntu for Snap! C++, but this trick should work on any system that runs systemd, so RedHat, Fedora, Debian should all be fine with this trick.

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Syndicate content

Snap! Websites
An Open Source CMS System in C++

Contact Us Directly