Create Directory Under /var/run at Boot on CentOS 7

centos7daemonlinuxstartup-scriptssystemd

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?

Best Answer

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:

RuntimeDirectory=foo

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 :

#Type Path            Mode UID      GID    Age Argument
d     /run/mydaemon   0755 myuser myuser   -   -

See the full tmpfiles.d docs here.

Related Topic