[英]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聯機幫助頁 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.