[LINUX] When I cut the directory for UNIX Socket under / var / run with systemd, I got stuck in a pitfall and what to do

Operation check environment

--CentOS7 server

** * Ubuntu 20.04 has the same implementation, so I think you'll be addicted to it as well **

Flow addicted to pitfalls

Thing you want to do

--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

What i did

--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)


Add the RuntimeDirectory setting to the systemd Unit file

According to the tmpfiles.d man page (https://www.freedesktop.org/software/systemd/man/tmpfiles.d.html#Description)


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


Reference link

Recommended Posts

When I cut the directory for UNIX Socket under / var / run with systemd, I got stuck in a pitfall and what to do
What I did when I got stuck in the time limit with lambda python
I got stuck when trying to specify a relative path with relative_to () in python
A reminder of what I got stuck when starting Atcoder with python
I referred to it when I got stuck in the django geodjango tutorial (editing)
Automate background removal for the latest portraits in a directory with Python and API
When I tried to do socket communication with Raspberry Pi, the protocol was different
What to do when only the window is displayed and nothing is displayed in pygame Note
What I did when I was angry to put it in with the enable-shared option
What to do if you run python in IntelliJ and end with an error
What I was addicted to when dealing with huge files in a Linux 32bit environment
I also tried to imitate the function monad and State monad with a generator in Python
What I was addicted to when I built my own neural network using the weights and biases I got with scikit-learn's MLP Classifier.
[For IT beginners] What to do when the rev command cannot be used with Git Bash
What to do when a part of the background image becomes transparent when the transparent image is combined with Pillow
A story that didn't work when I tried to log in with the Python requests module
What I did when I migrated to Linux 2 with EOL support for Amazon Linux (learning and failure stories)
[AWS] What to do when the ping command causes a "timeout"
What to do when you get "I can't see the site !!!!"
What to do when the value type is ambiguous in Python?
What should I do with the Python directory structure after all?
What to do if you get a "Wrong Python Platform" warning when using Python with the NetBeans IDE
[Python] What to do when PEP8 is violated in the process of importing from the directory added to sys.path
How to write what to do when an application is first displayed in Qt for Python with Designer
What to do if you get the error RuntimeError: Python is not installed as a framework when trying to use matplitlib and pylab in Python 3.3
What skills do I need to program with the FBX SDK Python?
Replace the directory name and the file name in the directory together with a Linux command.
What to do when the result downloaded via scrapy is in English
I got stuck in a flask application redirect with a reverse proxy in between
What to do when the warning "The environment is in consistent ..." appears in the Anaconda environment
What to do when a warning message is displayed in pip list
What I investigated in the process of expressing (schematicizing) containers in a nested frame with Jupyter and making it
The story that had nothing to do with partitions when I did disk backup with dd for the first time
In IPython, when I tried to see the value, it was a generator, so I came up with it when I was frustrated.
When I tried to use Python on WSL (windows subsystem for linux), it got stuck in Jupyter (solved)
Function to extract the maximum and minimum values ​​in a slice with Go
I got the date from the pub rice in Kagawa and drew a graph
What I was addicted to when creating a web application in a windows environment
I got an error when trying to run Hello World in Go language
What to do when a warning appears around Python integration in Neovim's CheckHealth
I get a strange window when I use the open directory dialog in Tkinter
When I tried to connect with SSH, I got a warning about free space.
I searched for the skills needed to become a web engineer in Python
What I thought and learned to study for 100 days at a programming school
What to do when [Errno 2] No such file or directory appears in Python
What to do when the graph does not appear in jupyter (ipython) notebook
What to do if a version error occurs in the selenium Chrome driver
[EC2] What to do when selenium is stuck and processing does not proceed
How to get a list of files in the same directory with python