简体   繁体   English

无法从 systemd 服务启动 pythonscript

[英]Cant start a pythonscript from systemd service

Here is the my systemd unit file.这是我的 systemd 单元文件。 I have a logging init script inside pythonscript.我在 pythonscript 中有一个日志记录初始化脚本。 Here happened a filenot found error.这里发生了一个找不到文件的错误。 At the same time it seems the python is from system python directory and not from my environment.同时,python 似乎来自系统 python 目录,而不是来自我的环境。 how to solve this.如何解决这个问题。

[Service]
User=nobody
Group=nogroup
ExecStart=/home/ubuntu/app/env/bin/python /home/ubuntu/app/backend/app.py --path=/tmp/app_1.sock

Systemd error log:系统错误日志:

:     loghandler = TimedRotatingFileHandler('logs/mainserver.log', when="midnight", i
:   File "/usr/lib/python3.6/logging/handlers.py", line 202, in __init__
:     BaseRotatingHandler.__init__(self, filename, 'a', encoding, delay)
:   File "/usr/lib/python3.6/logging/handlers.py", line 57, in __init__
:     logging.FileHandler.__init__(self, filename, mode, encoding, delay)
:   File "/usr/lib/python3.6/logging/__init__.py", line 1032, in __init__
:     StreamHandler.__init__(self, self._open())
:   File "/usr/lib/python3.6/logging/__init__.py", line 1061, in _open
:     return open(self.baseFilename, self.mode, encoding=self.encoding)
: FileNotFoundError: [Errno 2] No such file or directory: '/logs/mainserver.log'

Also please tell from which directory my script is running..另外请告诉我的脚本从哪个目录运行..

I resolved this by adding directive in [Service] part我通过在[Service]部分添加指令解决了这个问题

WorkingDirectory=/home/ubuntu/app/backend/

This is working without any errors.这工作没有任何错误。 I think now python env is used from my aioenv .我想现在 python env 是从我的aioenv中使用的。 Otherwise it aready should show some erros.否则它应该显示一些错误。 Also keep in mind do not put the WorkingDirectory before ExecStart= Hope this will help someone另请记住do not put WorkingDirectory放在ExecStart=之前,希望这会对某人有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM