Create a directory under /var/run at boot

Solution 1:

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.

Solution 2:

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