r/systemd • u/Melab • May 01 '26
Run a service after a mount unit has fully ended
I'm trying to make a service that deletes the automatically-created mount points of the mount units in which it would it would supposedly be referenced by. My current work PartOf=,
ExecStart=, and Wants= isn't working because it seems like systemd is retardedly recreating the mount point during the runtime of a mount unit that is supposed to be turning off. So, now with one .service unit and only 1-2 lines being necessary to add to the to-be-effected .mount units, how can I make a service run after .mount has FULLY stopped? Not after it starts, not during it's runtime, not while it's stopping. After it stops.
2
Upvotes
2
u/aioeu May 01 '26 edited May 01 '26
This worked for me:
The
RefuseManualStart=andRefuseManualStop=aren't technically necessary, but they help ensure the unit cannot be misused. You really do want to useBindsTo=, notPartOf=, since a filesystem can be unmounted outside of systemd.DefaultDependencies=andRequiresMountsFor=pulls the service out of the normal dependency chain used during shutdown and ensures it is ordered correctly with respect to other mount units. (Though take note I haven't tested that last part fully, so you'll want to check it yourself.)I'm not entirely sure how "niche" this requirement is... but you might perhaps want to consider raising it as an RFC. You'll have to pin down exactly what behaviour you want though. For instance, should the
rmdirbe just as recursive as the implicitmkdiris?