Path-Based Activation of systemd Units

SergeantBiggs

systemdsystem administrationlinuxarchlinuxsystemd

314 Words

2022-09-25 13:51 +0000


I recently learned about a really nice feature of systemd:

Path-based activation of units.

This feature allows systemd to monitor files or paths, and start a unit when the path exists, or something in it is changed. It has different options:

  • PathExists: triggers if path exists.
  • PathExistsGlob: triggers if a file that matches a glob exists.
  • PathModified: triggers if path is changed (every write).
  • PathChanged: triggers if path is changed (not every write, only when file is closed).
  • DirectoryNotEmpty: triggers if directory contains at least one file.

This is done (in typical systemd fashion), by creating a .path file that corresponds to a specific service file.

I’ve been using it for some of my units that have their data storage on an external mount point, that due to encryption, is only mounted manually after booting the machine.

One service that uses this is my Jellyfin instance. So, to make sure Jellyfin only starts after the volume is mounted (eg. the path exists), we can do the following thing:

First, we create a .path file:

systemctl edit --full --force jellyfin.path

In that path file, we include the following lines:

[Path]
PathExists=/media/zfsnas/cryptset/smb/shared

[Install]
WantedBy=multi-user.target

This is pretty much the simplest form of creating a .path unit. See the manpage for more options.

The important part is the WantedBy= in the install section. This (like with all units) allows us to enable the units, and will then start them when it enters multi-user mode.

We can then enable the .path file, and disable the regular .service file:

systemctl disable jellyfin.service
systemctl enable jellyfin.path

After that, jellyfin.service will wait for the specified path to exist before starting. I really like this feature, and I can already think of a few things that this would be handy for (monitoring files and directories, automating backups to external drives, etc).

I’ll keep you posted if I do anything interesting with it!

Articles from blogs I read

Mastodon Stories for systemd v262

On September 22 we released systemd v262 into the wild. In the weeks leading up to that release (and since then) I have posted a series of serieses of posts to Mastodon about key new features in this release, under the #systemd262 hash tag. In case you aren…

via Pid Eins October 15, 2026

"No way to prevent this" say users of only language where this regularly happens

In the hours following the release of CVE-2026-92392 for the project curl, site reliability workers and systems administrators scrambled to desperately rebuild and patch all their systems to fix a use-after-free in HTTP/2 upload retries whe…

via Xe Iaso's blog October 14, 2026

Why is Hacker News like that?

Hacker News (aka HN) is a link aggregator where “hackers”1 gather to discuss technology, politics, and anything which “good hackers would find interesting”. HN is a means by which its host, prominent startup incubator Y Combinator, projects soft…

via Drew DeVault's blog September 23, 2026

Notes on Arrow's Impossibility Theorem

Plurality voting is still bad, and finding a better mechanism is still hard.

via Ξ September 7, 2026

Generated by openring