79

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?

0

2 Answers 2

127

There are two alternatives.

One is to declare a RuntimeDirectory in the systemd unit file of your service. Example:

RuntimeDirectory=foo 

This will create /run/foo when the underlying systemd service starts. (Note: DO NOT provide a full path, just the path under /run) For full docs please see the appropriate entry in systemd.exec docs. Setting permissions or user/group ownership is a bit difficult (sometimes impossible) this way, but for a basic usage it works well. The directory is also automatically removed once the systemd service stops.


Alternatively, configure the automatic creation of such directories or files using tmpfiles.d. For example, /etc/tmpfiles.d/mydaemon.conf :

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

The dir will be created at boot, or if you run systemd-tmpfiles --create manually. If you are a packager for an OS, not a local user, then such a file should be put in /usr/lib/tmpfiles.d instead. See the full tmpfiles.d docs here.

3
  • 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-) Commented May 30, 2016 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. Commented Jul 22, 2019 at 5:00
  • That's because creation in /run disallowed for non-root users by default. Either systemctl edit <unit> and add a pre-start install -d … command to create necessary directories with correct permissions, or use tmpfiles.d to the same effect. Commented Oct 28, 2024 at 11:45
9

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:

[Unit] Description=MyDaemon Helper Simple Service After=network.target [Service] Type=simple ExecStartPre=-/usr/bin/mkdir /var/run/mydaemon ExecStart=/usr/bin/chown myuser:myuser /var/run/mydaemon Restart=on-abort [Install] WantedBy=multi-user.target 

Then I started this service:

systemctl start mydaemon-helper

systemctl status mydaemon-helper

Output:

[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

5
  • 1
    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! Commented May 30, 2016 at 3:22
  • 4
    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. :) Commented Oct 25, 2017 at 13:24
  • 6
    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. Commented Oct 28, 2017 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. Commented Oct 7, 2018 at 5:11
  • Why does the ExecStartPre command need a - ? Commented Jan 17, 2024 at 18:03

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.