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?


Two alternatives to have systemd create directories, typically the easiest is to declare a RuntimeDirectory in the unit file of your service:

RuntimeDirectory=, RuntimeDirectoryMode= Takes a list of directory names. If set, one or more directories by the specified names will be created below /run (for system services) or below $XDG_RUNTIME_DIR (for user services) when the unit is started, and removed when the unit is stopped. The directories will have the access mode specified in RuntimeDirectoryMode=, and will be owned by the user and group specified in User= and Group=. Use this to manage one or more runtime directories of the unit and bind their lifetime to the daemon runtime. The specified directory names must be relative, and may not include a "/", i.e. must refer to simple directories to create or remove. This is particularly useful for unprivileged daemons that cannot create runtime directories in /run due to lack of privileges, and to make sure the runtime directory is cleaned up automatically after use.

Or 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   -   -
|improve this answer|||||
  • I used the latter because the actual daemon uses the systemd-sysv-generator and I've had enough learning curves for the week. Just that one .conf file and that one line. Feelin good right now B-) – user24601 May 30 '16 at 3:17
  • I've already had the latter defined in my /usr/lib/tmpfiles.d/php7.3-fpm.conf and /usr/lib/tmpfiles.d/php7.2-fpm.conf and it still doesn't create the /run/php directory. – MarthyM Jul 22 '19 at 5:00

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:

Description=MyDaemon Helper Simple Service

ExecStartPre=-/usr/bin/mkdir /var/run/mydaemon
ExecStart=/usr/bin/chown myuser:myuser /var/run/mydaemon


Then I started this service:

systemctl start mydaemon-helper

systemctl status mydaemon-helper


[root@alpha etc]# systemctl status mydaemon-helper.service
● mydaemon-helper.service - MyDaemon Helper Simple Service
   Loaded: loaded (/etc/systemd/system/mydaemon-helper.service; disabled; vendor preset: disabled)
   Active: inactive (dead)

May 28 20:53:50 alpha systemd[1]: Starting MyDaemon Helper Simple Service...
May 28 20:53:50 alpha systemd[1]: Started MyDaemon Helper Simple Service.

Lastly I told the system to load it on startup:

systemctl enable mydaemon-helper

|improve this answer|||||
  • Ok, I guess I should have placed this in my question considering I'm such a noob. I'm learning though--thanks to you guys! – user24601 May 30 '16 at 3:22
  • 3
    No, creating an answer was the right thing to do, so people can comment on it and also so it doesn't clutter up your question. Answering your own questions is explicitly encouraged on SO. And your answer isn't wrong either, it's just that there are much better ways of doing this, so IMO you shouldn't have been downvoted. It shouldn't be upvoted either. :) – Bryan Larsen Oct 25 '17 at 13:24
  • 3
    While RuntimeDirectory is a better way of doing this now, I ran into a server with an old version of systemd (208) where that directive doesn't exist, so this answer is the only workaround. – Davor Cubranic Oct 28 '17 at 13:54
  • This also still seems to be the way to do it if you need a directory created and owned by a different user than the service user, or for the directory to persist until reboot, or for the directory to be shared between multiple services. – Perkins Oct 7 '18 at 5:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.