为什么 /dev/shm 文件夹会在 Ubuntu 18.04 中定期清理?
Why may /dev/shm folder be periodically cleaned in Ubuntu 18.04?
我有一个与 /dev/shm 文件夹一起使用的 python 应用程序。
它的进程不断地在它自己的 /dev/shm/ 子文件夹中创建、读取和删除二进制文件。
在大多数 Ubuntu 服务器上,它工作正常,没有任何错误,内存泄漏。
但是有一个 Ubuntu 服务器有一个非常奇怪的行为。
有时大约每小时(但不是严格定期)整个 /dev/shm 文件夹都会被清理一次。
例如,来自应用程序或手动创建的所有文件和文件夹都消失了。
应用程序本身不会这样做,没有引入其他应用程序、服务、守护程序来有意这样做。该应用程序不能超过 20M,这离限制太远了。
tmpfs 7.8G 8.3M 7.8G 1% /dev/shm
有什么想法可能是问题的根本原因吗?
看这里:
superuser.com: Why are the contents of /dev/shm/ is being removed automatically
Ask Ubuntu: 16.04 LTS and /dev/shm/ Files Disappearing
After hours of searching and reading, I found the culprit. It's a
setting for systemd. The /etc/systemd/logind.conf contains default
configuration options, with each of them commented out. The RemoveIPC
option is set to yes by default. That option tells systemd to clean up
interprocess communication (IPC) for "user accounts" who aren't logged
in. This does not affect "system accounts"
In my case, the files and directories were being created for a user
account, not a system account.
There are two possible solutions:
Create the files with/for a system user -- a user created with the system option (adduser -r or adduser --system)
Edit /etc/systemd/logind.conf, uncomment the line RemoveIPC=yes, change it to RemoveIPC=no, save, and reboot the system
In my case, I went with option #2 because the user was already
created.
我有一个与 /dev/shm 文件夹一起使用的 python 应用程序。 它的进程不断地在它自己的 /dev/shm/ 子文件夹中创建、读取和删除二进制文件。 在大多数 Ubuntu 服务器上,它工作正常,没有任何错误,内存泄漏。
但是有一个 Ubuntu 服务器有一个非常奇怪的行为。 有时大约每小时(但不是严格定期)整个 /dev/shm 文件夹都会被清理一次。 例如,来自应用程序或手动创建的所有文件和文件夹都消失了。
应用程序本身不会这样做,没有引入其他应用程序、服务、守护程序来有意这样做。该应用程序不能超过 20M,这离限制太远了。
tmpfs 7.8G 8.3M 7.8G 1% /dev/shm
有什么想法可能是问题的根本原因吗?
看这里:
superuser.com: Why are the contents of /dev/shm/ is being removed automatically
Ask Ubuntu: 16.04 LTS and /dev/shm/ Files Disappearing
After hours of searching and reading, I found the culprit. It's a setting for systemd. The /etc/systemd/logind.conf contains default configuration options, with each of them commented out. The RemoveIPC option is set to yes by default. That option tells systemd to clean up interprocess communication (IPC) for "user accounts" who aren't logged in. This does not affect "system accounts"
In my case, the files and directories were being created for a user account, not a system account.
There are two possible solutions:
Create the files with/for a system user -- a user created with the system option (adduser -r or adduser --system)
Edit /etc/systemd/logind.conf, uncomment the line RemoveIPC=yes, change it to RemoveIPC=no, save, and reboot the system
In my case, I went with option #2 because the user was already created.