--CentOS7 server
** * Ubuntu 20.04 has the same implementation, so I think you'll be addicted to it as well **
--I want to build an API server for an application called hoge
--The front desk receives the request with nginx
and passes it to the application
--hoge
is daemonized with systemd
--hoge
receives a request from nginx
on ʻUNIX Socket --ʻUNIX Socket
is /var/run/hoge/hoge.sock
--Create a / var / run / hoge
directory
--Permissions are hoge: hoge
--Place various configuration files of hoge
and nginx
and start the daemon
--I was able to confirm that it worked at this point
--Restart the server
--When you hit the API again, nginx
returned a Bad Gateway
error
--When I looked it up, /var/run/hoge/hoge.sock
was missing for each directory.
--In CentOS7, the file under / var / run
is tmpfs
, and when the server is restarted, the files under / var / run
disappear.
# /var/run is/Symbolic link to run
$ ls -ld /var/run
lrwxrwxrwx. 1 root root 6 Jul 25 2019 /var/run -> ../run
# /run mounts tmpfs
$ mount | grep /run
tmpfs on /run type tmpfs (rw,nosuid,nodev,mode=755)
RuntimeDirectory
setting to the systemd Unit fileAccording to the tmpfiles.d
man page (https://www.freedesktop.org/software/systemd/man/tmpfiles.d.html#Description)
Original
System daemons frequently require private runtime directories below /run to store communication sockets and similar.
For these, it is better to use RuntimeDirectory= in their unit files (see systemd.exec(5) for details), if the flexibility provided by tmpfiles.d is not required.
The advantages are that the configuration required by the unit is centralized in one place, and that the lifetime of the directory is tied to the lifetime of the service itself.
Similarly, StateDirectory=, CacheDirectory=, LogsDirectory=, and ConfigurationDirectory= should be used to create directories under /var/lib/, /var/cache/, /var/log/, and /etc/.
tmpfiles.d should be used for files whose lifetime is independent of any service or requires more complicated configuration.
Japanese translation
The system daemon is used to store communication sockets, etc./Often you need a private runtime directory under run.
For these, tmpfiles.If you don't need the flexibility provided by d, the RuntimeDirectory in the unit file=It is recommended to use (for more information, systemd.See exec (5)).
The advantage is that the configuration required for the unit is centralized in one place, and the lifetime of the directory is associated with the lifetime of the service itself.
Similarly/var/lib/、/var/cache/、/var/log/,and/etc/To create a directory under, StateDirectory=、CacheDirectory=、LogsDirectory=,andConfigurationDirectory=Must be used.
tmpfiles.d should be used for files whose lifetime is service independent or requires a more complex configuration.
Since there is, add the setting of RuntimeDirectory
.
Also, since we want to set the permission to 0755
this time, set RuntimeDirectoryMode
as follows.
Lines added to the UNIT file
RuntimeDirectory=hoge
RuntimeDirectoryMode=0755
systemd.exec
(https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RuntimeDirectory=), / run
is the starting point, so the full path Specify the directory name you want to create under / run
insteadRecommended Posts