Systemd 服务不执行我的 Python 脚本

Systemd service not executing my Python script

我有我的 python 脚本来安排每隔 1 分钟创建一个文本文件。我想 运行 这个文件在后台,即使在重新启动系统后它应该还活着。

我的 Python 文件:

import schedule
import time

#datetime.datetime.now().strftime("%H:%M")


def job():
    print("Scheduling is Working...")
    createfile()

def createfile():
    company = "Example file"
    with open('company.txt', 'w+') as f:
            f.write(str(company))
            print("File Created on:",time.ctime(time.time()))
            f.close()
    return True
# schedule.every(10).minutes.do(job)
# schedule.every().hour.do(job)

#schedule.every().day.at("11.40").do(job)
schedule.every(1).minutes.do(job)

while 1:
    schedule.run_pending()
    time.sleep(1)

我的 Systemd 服务:

[Unit]
Description=Run scheduler back
After=multi-user.target
Conflicts=getty@tty1.service

[Service]
Type=simple
ExecStart=/usr/bin/python3 /var/www/html/dev/schedulerun.py > /var/log/sanu_daemon.log 2>&1
StandardInput=tty-force

[Install]
WantedBy=multi-user.target

我检查了状态,它工作正常但没有创建文本文件 我不知道错误是什么。

您可以在服务部分配置工作目录和用户:

[Service]
WorkingDirectory=/var/www/html/dev/
User=frank
# or www-data, or whatever user you want ...

# Other settings, such as ...
Type=simple
ExecStart=/usr/bin/python3 /var/www/html/dev/schedulerun.py > /var/log/sanu_daemon.log 2>&1
StandardInput=tty-force

有关详细信息,请参阅 systemd.exec manpage