Python 脚本仅当作为服务运行时才会失败

Python 脚本仅当作为服务运行时才会失败

我有一个自己编写的 Python 脚本。当我运行 ./myscript.py 时,它按预期完美运行,没有出错,并持续运行数天,直到我手动将其关闭。

我还为我的 Python 脚本创建了一个 SystemD 服务。但是,运行该服务时,每隔一轮就会中途出错。SystemD 重新启动脚本,它在 1 轮中运行正常,但在中途出错。

以下是 journalctl -xe -f 的错误:

Jul 05 16:04:35 glas-dev systemd[1]: magewelk.service: Start 
operation timed out. Terminating.
Jul 05 16:04:35 glas-dev magewelk.py[9685]:       
Jul 05 16:04:35 glas-dev systemd[1]: magewelk.service: Main process 
exited, code=dumped, status=3/QUIT
Jul 05 16:04:35 glas-dev systemd[1]: Failed to start Data collector 
Magewell to Elasticsearch.
-- Subject: Unit magewelk.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-
devel
-- 
-- Unit magewelk.service has failed.
-- 
-- The result is failed.
Jul 05 16:04:35 glas-dev systemd[1]: magewelk.service: Unit entered 
failed state.
Jul 05 16:04:35 glas-dev systemd[1]: magewelk.service: Failed with 
result 'core-dump'.
Jul 05 16:04:35 glas-dev systemd[1]: magewelk.service: Service hold-
off time over, scheduling restart.
Jul 05 16:04:35 glas-dev systemd[1]: Stopped Data collector Magewell 
to Elasticsearch.
-- Subject: Unit magewelk.service has finished shutting down
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-
devel
-- 
-- Unit magewelk.service has finished shutting down.
Jul 05 16:04:35 glas-dev systemd[1]: Starting Data collector Magewell 
to Elasticsearch...
-- Subject: Unit magewelk.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-
devel
-- 
-- Unit magewelk.service has begun starting up.

magewelk.service文件如下:

[Unit]
Description=Data collector Magewell to Elasticsearch
After=syslog.target

[Service]
WorkingDirectory=/opt/labglas_streamer
ExecStartPre=/bin/bash -c 'chown -R charter:www-data 
/opt/labglas_streamer'
ExecStart=/opt/labglas_streamer/magewelk.py
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all

[Install]
WantedBy=multi-user.target

该脚本是单线程的,带有错误捕获功能。我没有从该脚本中收到任何错误。

这是在 Ubuntu 16.04.2 LTS 服务器上。我观察了服务崩溃时的内存、网络连接、处理器,没有发现任何异常。

答案1

您可能希望使用“简单”作为类型,而不是“通知”。仅当您的 Python 脚本知道如何通知 systemd 时,通知才有效。

相关内容