微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ubuntu – 如何使用systemd服务或tmpfiles.d自动创建运行时文件夹?

我正在尝试在/ run / gunicorn为一些Gunicorn套接字/ PID文件创建一个运行时文件夹,这些文件适用于Django应用程序.如果我手动创建目录,我可以使一切正常.但是,我正在尝试使其成为一个强大的设置,并最终使用Ansible自动化所有内容.

我想我有两个选项,基于这个question.

选项1 – RuntimeDirectory

我认为第一个选项是在我的systemd服务文件中使用RuntimeDirectory =,但我无法创建该文件夹.服务文件包含:

#/etc/systemd/system/gunicorn_django_test.service
[Unit]
Description=gunicorn_django daemon
After=network.target

[Service]
User=gunicorn
Group=www-data
RuntimeDirectory=gunicorn #This line is supposed to create a directory
RuntimeDirectoryMode=755
PIDFile=/run/gunicorn/django_test_pid
WorkingDirectory=/vagrant/webapps/django_venv/django_test
ExecStart=/vagrant/webapps/django_venv/bin/gunicorn --pid /run/gunicorn/django_test_pid --workers 3 --bind unix:/run/gunicorn/django_test_socket django_test.wsgi --error-logfile /var/log/gunicorn/django_test_error.log
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

当我运行systemctl start gunicorn_django_test.service时,服务无法启动.当我剪掉exec行并手动运行时,我得到错误:/ run / gunicorn不存在.无法创建pidfile.如果我手动创建/ run / gunicorn文件夹,我可以开始工作.

选项2 – tmpfiles.d

第二个选项是使用tmpfiles.d在启动时创建一个文件夹,为pid / socket文件做好准备.我试过这个文件

#/etc/tmpfiles.d/gunicorn.conf
d /run/gunicorn 0755 gunicorn www-data -

这会创建一个目录,但会以某种方式快速删除,当我启动该服务时,该文件夹不可用.

我可以手动将一些PreExec mkdir命令添加到服务文件中,但我想了解为什么RuntimeDirectory / tmpfiles.d无法正常工作.谢谢.

版本/信息:
Ubuntu 16.04 Server / systemd 229 / Gunicorn 19.7.1 / runtime dir = / run

解决方法:

我在PermissionsstartOnly = True中添加了并按照建议为每个服务设置了一个运行时文件夹.我还在文件夹模式的开头添加了0.

[Unit]
Description=gunicorn_django daemon
After=network.target

[Service]
PermissionsstartOnly=True
User=gunicorn
Group=www-data
RuntimeDirectory=gunicorn_django
RuntimeDirectoryMode=0775
PIDFile=/run/gunicorn_django/django_test_pid
WorkingDirectory=/vagrant/webapps/django_venv/django_test
ExecStart=/vagrant/webapps/django_venv/bin/gunicorn --pid /run/gunicorn_django/django_test_pid --workers 3 --bind unix:/run/gunicorn_django/django_test_socket django_test.wsgi --error-logfile /var/log/gunicorn/django_test_error.log
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target

它现在正在创建具有正确权限的文件夹.

drwxrwxrw-  2 gunicorn www-data   40 Mar 30 07:11 gunicorn_django/

谢谢@quixotic和@ mark-stosberg

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐