unantu下的tmp文件夹_ubuntu – 如何使用systemd服务或tmpfiles.d自动创建运行时文件夹?...

本文介绍了在Ubuntu 16.04上为Gunicorn服务创建/ run / gunicorn目录的问题。作者探讨了使用systemd服务的RuntimeDirectory选项和tmpfiles.d配置文件的两个方法,以及遇到的挑战和解决方案。通过在systemd服务文件中添加PermissionsStartOnly=True和修改RuntimeDirectoryMode,成功实现了运行时目录的自动创建。
摘要由CSDN通过智能技术生成

我正在尝试在/ 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.icode9.com/content-3-385201.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值