I had a daemon that needed its own dir in /var/run
for its PID file with write permission granted to the daemon's user.
I found I could create this dir with these commands:
# mkdir /var/run/mydaemon
Then I could change its ownership to the user/group under which I wished to run the process:
# chown myuser:myuser /var/run/mydaemon
But this dir would be GONE whenever I issue a reboot! How do I get this dir to create every time the machine boots?
There are two alternatives to have systemd create directories under
/var/run
//run
.Typically the easiest is to declare a
RuntimeDirectory
in the unit file of your service. Example:This will create
/var/run/foo
for a system unit. (Note: DO NOT provide a full path, just the path under/var/run
) For full docs please see the appropriate entry in systemd.exec docs.For runtime directories that require more complex or different configuration or lifetime guarantees, use
tmpfiles.d
and have your package drop a file/usr/lib/tmpfiles.d/mydaemon.conf
:See the full tmpfiles.d docs here.
I created a service that would make the dir at start:
vim /etc/systemd/system/mydaemon-helper.service
The contents of
/etc/systemd/system/mydaemon-helper.service
:Then I started this service:
systemctl start mydaemon-helper
systemctl status mydaemon-helper
Output:
Lastly I told the system to load it on startup:
systemctl enable mydaemon-helper