gpt4 book ai didi

python - 将 python 脚本作为 systemd 服务运行

转载 作者:太空狗 更新时间:2023-10-30 01:26:43 27 4
gpt4 key购买 nike

我有一个 python 脚本 myScript.py,它每 2 秒写入一个文件。但是当我想将此脚本作为 systemd 服务运行时,服务可以工作但不能写入文件。

我在 /lib/systemd/system/ 上创建了一个 myscript.service 文件设计如下:

[Unit]
Description=My Script Service
After=multi-user.target

[Service]
Type=idle
ExecStart=/usr/bin/python /home/pala/PycharmProjects/myScript.py

[Install]
WantedBy=multi-user.target

myScript.py是:

import time
while True:

with open("/home/pala/Documents/file.txt", "a") as myFile:
myFile.write("--**--")

time.sleep(2)

最佳答案

也许在 myscript.service 添加一个工作目录会有所帮助:

[Service]
(...)
WorkingDirectory=/home/pi/your_working_directory

最好的问候基连

关于python - 将 python 脚本作为 systemd 服务运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42735934/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com