64

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?

HBruijn
  • 72,524
  • 21
  • 127
  • 192
user24601
  • 873
  • 1
  • 6
  • 8

2 Answers2

97

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.

Greg Dubicki
  • 1,191
  • 1
  • 14
  • 30
HBruijn
  • 72,524
  • 21
  • 127
  • 192
  • 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 03: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 05:00
5

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

user24601
  • 873
  • 1
  • 6
  • 8
  • 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 '16 at 03: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. :) – Bryan Larsen Oct 25 '17 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. – 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 07 '18 at 05:11