Mark Stosberg's accepted answer is entirely correct and gladly upvoted: this is impossible by design. But I agree that masking your own service temporarily could be useful once in a lifetime—for example, while debugging, or because a piece of hardware is temporarily out of working order.
But since the endless discussion is endless, fake it if can't make it: rename your.service
to your.service.masked
and do systemctl daemon-reload
so that systemd notices. The only downside is the service is not reported at all instead of existing but masked¹.
$ sudo mv -v /etc/systemd/system/dpkg-daily.service{,.masked}
renamed '/etc/systemd/system/dpkg-daily.service' -> '/etc/systemd/system/dpkg-daily.service.masked'
$ sudo systemctl daemon-reload
$ journalctl -n5
. . .
Oct 06 18:31:16 [previous irrelevant message, logged 5.5 hours ago...
... systemd itself is totally nonchalant about something ending in .masked ...]
Oct 06 23:53:57 buba systemd[1]: Reloading.
$ systemctl status dpkg-daily.service
Unit dpkg-daily.service could not be found. <== If you can live with that...
... and failed dependencies—but they would still fail if the unit were masked anyway. Note that you have no reason to mask a service unless it's started as a dependency of some other unit—just disable it in this case, as is recommended in the accepted answer.
$ sudo mv -v /etc/systemd/system/dpkg-daily.service{.masked,}
renamed '/etc/systemd/system/dpkg-daily.service.masked' -> '/etc/systemd/system/dpkg-daily.service'
$ sudo systemctl daemon-reload
$ systemctl status dpkg-daily.service
● dpkg-daily.service - Daily dpkg database backup
Loaded: loaded (/etc/systemd/system/dpkg-daily.service; static)
Active: inactive (dead) since Fri 2023-10-06 00:00:07 PDT; 24h ago
TriggeredBy: 🔴 dpkg-daily.timer <=== A red dot for a failed dependency. The red Emoji is fake, of course.
. . .
The matching .timer
wasn't extremely happy about its disappeared dependency. Since the dependency has been "unmasked" now, reset failed units:
$ systemctl list-units --failed
UNIT LOAD ACTIVE SUB DESCRIPTION
● dpkg-daily.timer loaded failed failed Daily dpkg database backup
. . .
$ sudo systemctl reset-failed
$ systemctl list-units --failed
UNIT LOAD ACTIVE SUB DESCRIPTION
0 loaded units listed.
Two commands instead of one (mv
and ...daemon-reload
) aren't that bad. The closest to masking you can probably get.
¹ I have no idea how I got this service in /etc/systemd/system
; I probably wrote it while getting rid of cron in Debian 10, back around the 2nd century BC, before upgrading to this distro, which is Debian 11 and long overdue an upgrade to 12, but I have no time. Newer Debians come with timers and services. A whole cron process for one daily and one weekly jobs felt kinda wasteful...
FWIW, such renaming is one of the ideas proposed in that thread, and the most acceptable by systemd design principles, as I understand them. This feature, if implemented, would be rather extremely rarely used, thus needs to be implemented using as few CPU cycles as possible, and, heaven save, without additional disk reads to determine if a unit is masked—but systemd reads the whole directory anyway.