python - 重启后系统服务不会启动

标签 python ubuntu systemd

关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。












我们不允许在 Stack Overflow 上提出有关通用计算硬件和软件的问题。您可以编辑问题,使其成为 on-topic对于堆栈溢出。


1年前关闭。







Improve this question




我大家,
我的服务有问题。我想在启动时启动一个 python 脚本(完成这个脚本大约需要 20 秒),我想每 2 分钟执行一次。所以我创建了这个服务:

[Unit]
Description=Set and check time and date of the board

[Service]
Type=simple
Restart=always
RestartSec=120
User=root
Group=root
ExecStart=/usr/bin/python /home/dev/Configuration-Folder/daemons/configureTimeScript.py

[Install]
WantedBy=multi-user.target
当我使用“sudo python/home/dev/Configuration-Folder/daemons/configureTimeScript.py”启动脚本时,它运行良好,但是当我重新启动 Ubuntu(16.04)时,该服务将无法启动并显示它处于非事件状态(死的)...
你能给我一些提示来解决这个问题吗?
谢谢
更新 1
当我想使用“sudo systemctl start setDateTime.service”手动启动服务时,服务按预期启动并保持“事件(运行)”,这很好
更新 2
我启用了该服务并重新启动了 Ubuntu,但没有改变

最佳答案

我编写了一个示例 systemd 服务,它执行每 10 秒运行一次的 Python 脚本。
Python 脚本位于 /opt/hello.py :

#!/bin/env python3

import os

os.system("echo 'Hello' > /home/telmo/Desktop/hello.txt")
注意文件开头的 shebang,我还使用 chmod +x /opt/hello.py 使脚本可执行.
完成这两个步骤后,我现在可以从命令行简单地调用脚本 /opt/hello.py .
然后我创建了服务单元文件( hello.service ):
[Unit]
Description=My Python Script

[Service]
Type=simple
ExecStart=/opt/hello.py
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target
并将其移动到正确的文件夹 sudo mv hello.service /etc/systemd/system .
完成后,我只需要启用该服务并首次启动它:
sudo systemctl enable hello
sudo systemctl start hello 
现在是 hello.txt每 10 秒在我的用户桌面上创建一次。尝试删除它,它将再次生成。由于我们启用了该服务,该行为将在系统重新启动后持续存在。

关于python - 重启后系统服务不会启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62832339/

相关文章:

Python Beautiful Soup 查找字符串并提取以下字符串

python - 错误全局变量数独

Python:如何摆脱从文件中读取的非 ASCII 字符

python - 单例数组 array(<function train at 0x7f3a311320d0>, dtype=object) 不能被视为有效集合

cron - 如何强制 chrony 立即同步?

maven - 更改 Kurento 项目证书时遇到问题?

php - 错误 : Couldn't open PDF file 'MJVQKFFGFEPBI.TMP' for writing (permission denied)

node.js - 在 Ubuntu 上安装 Node 包 "Pre-built binaries not installable"时出现 `grpc-js`

django - 如何运行多个 Django App Gunicorn systemd?

docker - 从 docker 容器中结构化日志记录到日志