简体   繁体   English

在 systemd 服务中启动时 Python 子进程库失败

[英]Python subprocess library fails when launch in systemd service

I am using the function subprocess.run and subprocess.check_output that works great when I launch my python code.我使用的函数subprocess.runsubprocess.check_output在启动 python 代码时效果很好。 I then create a service in systemd but the process crashes when getting to these functions然后我在 systemd 中创建了一个服务,但是在获取这些功能时进程崩溃了

From the log journalctl:从日志 journalctl:

Nov 30 19:19:24 srv2 python3[3369]:
   File "/usr/lib/python3.6/subprocess.py", line 423, in run
Nov 30 19:19:24 srv2 python3[3369]:
     with Popen(*popenargs, **kwargs) as process:
Nov 30 19:19:24 srv2 python3[3369]:
   File "/usr/lib/python3.6/subprocess.py", line 729, in __init__
Nov 30 19:19:24 srv2 python3[3369]:
     restore_signals, start_new_session)
Nov 30 19:19:24 srv2 python3[3369]:
   File "/usr/lib/python3.6/subprocess.py", line 1364, in _execute_child
Nov 30 19:19:24 srv2 python3[3369]:
     raise child_exception_type(errno_num, err_msg, err_filename)
Nov 30 19:19:24 srv2 python3[3369]:
 FileNotFoundError: [Errno 2] No such file or directory: 'mosquitto_passwd': 'mosquitto_passwd'

For some reason the service goes thru this morning, I added Type=simple and set user to root but not sure if it was the cause由于某种原因,服务今天早上通过,我添加了Type=simple并将用户设置为 root 但不确定是否是原因

[Unit]
Description=my service
After=network.target

[Service]
Type=simple
User=root
Group=root
Restart=always
WorkingDirectory=/home/python/service
ExecStart=/usr/bin/python3 /home/python/service/main.py

[Install]
WantedBy=multi-user.target

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

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