_note:原始发布的问题已被编辑,其面貌发生了很大变化!
概括:
使用 apshceduler 的 Python 脚本无法作为服务加载,退出代码为 staus1。需要注意的是,设计为每 10 秒运行一次的相同脚本while True:
(不使用 apscheduler 来执行相同的任务)语法作为守护进程(服务)运行得很好。
细节
我写了一个简单的 test.py 程序。它每 10 秒附加一些带有“some”字符串的 a.txt 文件。当我尝试将其作为守护程序服务运行时,会抛出错误代码。 。就其本身而言,无需将其用作守护进程(服务),代码就可以正常工作。另外,如果我在编写 test.py 文件时不使用 apscheduler,则服务运行顺利。
我将把所有代码放在这里以获取详细信息,包括systemctl status
#!/usr/bin/env python3
import os, glob, shutil
from datetime import datetime
from apscheduler.schedulers.blocking import BlockingScheduler
sched = BlockingScheduler()
def test():
appendFile = open(r'/home/sangharsh/code/a.txt', 'a')
appendFile.write("Jai Bhim \n" )
appendFile.close()
sched.add_job(test, 'interval', seconds=10)
sched.start()
它位于要/home/sangharsh/code/workingWithFiles/
作为守护程序运行它,我创建服务文件 -
[Unit]
Description=Test Service
After=multi-user.target
[email protected]
[Service]
Type=simple
ExecStart=/usr/bin/env python3 /home/sangharsh/code/workingWithFiles/test.py
StandardInput=tty-force
[Install]
WantedBy=multi-user.target
该文件位于/lib/systemd/system/
.
我重新启动守护进程:
sudo systemctl daemon-reload
然后启用 test.service 文件 -
sudo systemctl enable test.service
并启动 test.service
sudo systemctl start test.service
并检查状态
sudo systemctl status test.service
演出节目systemctl status
:
● test.service - Test Service
Loaded: loaded (/lib/systemd/system/test.service; enabled; vendor preset: ena
Active: failed (Result: exit-code) since Tue 2019-07-09 22:57:31 IST; 8min ag
Process: 4750 ExecStart=/usr/bin/env python3 /home/sangharsh/code/workingWithF
Main PID: 4750 (code=exited, status=1/FAILURE)
Jul 09 22:57:31 sangharsh-HP-240-G4-Notebook-PC systemd[1]: Started Test Service
Jul 09 22:57:31 sangharsh-HP-240-G4-Notebook-PC systemd[1]: test.service: Main p
Jul 09 22:57:31 sangharsh-HP-240-G4-Notebook-PC systemd[1]: test.service: Failed
'sudo Journalctl -xe' 的输出如下:
Subject: Unit failed
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
--
-- The unit test.service has entered the 'failed' state with result 'exit-code'.
Jul 13 02:12:23 sangharsh-HP-240-G4-Notebook-PC sudo[18333]: sangharsh : TTY=pts/0 ; PWD=/home/sangharsh ; USER=root ; COMMAND=/bin/systemctl status test.service
Jul 13 02:12:23 sangharsh-HP-240-G4-Notebook-PC sudo[18333]: pam_unix(sudo:session): session opened for user root by (uid=0)
Jul 13 02:12:38 sangharsh-HP-240-G4-Notebook-PC systemd-resolved[856]: Server returned error NXDOMAIN, mitigating potential DNS violation DVE-2018-0001, retrying transaction with reduced feature level UDP.
Jul 13 02:13:11 sangharsh-HP-240-G4-Notebook-PC sudo[18333]: pam_unix(sudo:session): session closed for user root
Jul 13 02:13:53 sangharsh-HP-240-G4-Notebook-PC sudo[18364]: sangharsh : TTY=pts/0 ; PWD=/home/sangharsh ; USER=root ; COMMAND=/usr/bin/pip3 install apscheduler
Jul 13 02:13:53 sangharsh-HP-240-G4-Notebook-PC sudo[18364]: pam_unix(sudo:session): session opened for user root by (uid=0)
Jul 13 02:13:55 sangharsh-HP-240-G4-Notebook-PC sudo[18364]: pam_unix(sudo:session): session closed for user root
Jul 13 02:14:13 sangharsh-HP-240-G4-Notebook-PC systemd-resolved[856]: Server returned error NXDOMAIN, mitigating potential DNS violation DVE-2018-0001, retrying transaction with reduced feature level UDP.
Jul 13 02:15:13 sangharsh-HP-240-G4-Notebook-PC sudo[18378]: sangharsh : TTY=pts/0 ; PWD=/home/sangharsh ; USER=root ; COMMAND=/bin/journalctl -xe
Jul 13 02:15:13 sangharsh-HP-240-G4-Notebook-PC sudo[18378]: pam_unix(sudo:session): session opened for user root by (uid=0)
Jul 13 02:16:02 sangharsh-HP-240-G4-Notebook-PC sudo[18378]: pam_unix(sudo:session): session closed for user root
Jul 13 02:16:32 sangharsh-HP-240-G4-Notebook-PC sudo[18388]: sangharsh : TTY=pts/0 ; PWD=/home/sangharsh ; USER=root ; COMMAND=/bin/journalctl -xe test.service
Jul 13 02:16:32 sangharsh-HP-240-G4-Notebook-PC sudo[18388]: pam_unix(sudo:session): session opened for user root by (uid=0)
Jul 13 02:16:32 sangharsh-HP-240-G4-Notebook-PC sudo[18388]: pam_unix(sudo:session): session closed for user root
Jul 13 02:16:44 sangharsh-HP-240-G4-Notebook-PC sudo[18390]: sangharsh : TTY=pts/0 ; PWD=/home/sangharsh ; USER=root ; COMMAND=/bin/journalctl -xe
Jul 13 02:16:44 sangharsh-HP-240-G4-Notebook-PC sudo[18390]: pam_unix(sudo:session): session opened for user root by (uid=0)
~
~
我提到这问题。我尝试了这些选项,但没有帮助。
请指导。
一些澄清
一些澄清以避免任何混淆 -
- 我尝试运行 test.py 脚本,它工作正常。它可以将消息写入“a.txt”文件。所以它的代码成功了。
- 我也尝试了另一种方法。我尝试删除“apscheduler”并改为使用
while True
语法。这样我就能够运行它的守护进程。在这种情况下, 的输出systemctl status
处于活动状态。在向 ugo 授予“a.txt”文件的 rwx 权限后,我能够将其作为服务文件运行(如上面的摘要所述)
答案1
systemd 单元文件中的这一行仍然是错误的:
ExecStart=/usr/bin/env/python3 /home/sangharsh/code/workingWithFiles/test.py
/usr/bin/env/python3
我很确定您的系统上没有。这与穆鲁的第一条评论中的问题相同。
答案2
更改BlockingScheduler
为BackgroundScheduler
因为它需要在后台运行。
BlockingScheduler
需要 systemd 中不可用的活动终端。