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
Related videos on Youtube
user24601
Updated on September 18, 2022Comments
-
user24601 over 1 year
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?
-
user24601 almost 8 yearsI 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 almost 8 yearsOk, I guess I should have placed this in my question considering I'm such a noob. I'm learning though--thanks to you guys!
-
Bryan Larsen over 6 yearsNo, 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. :)
-
Davor Cubranic over 6 yearsWhile
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. -
MarthyM almost 5 yearsI'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.