74

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

119

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.

2
  • 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, 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.
    – MarthyM
    Jul 22, 2019 at 5:00
7

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!
    – user24601
    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. :) Oct 25, 2017 at 13:24
  • 5
    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. 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.
    – Perkins
    Oct 7, 2018 at 5:11
  • Why does the ExecStartPre command need a - ?
    – Vik
    Jan 17 at 18:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .