分享

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

 印度阿三17 2019-08-10

我正在尝试在/ 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

来源:https://www./content-3-385201.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多