我创建一个文件 job_tracking.py
#!/home/thao/venv/bin/python
while True:
print("hello")
# job_tracking_stats()
log_app('Stats tracking Sleeping...')
time.sleep(5)
log_app('Stats tracking Wakeup...')
以及 job_tracking.service
[Unit]
Description=Test Service
After=multi-user.target
[email protected]
[Service]
Type=simple
User=thao
ExecStart=/home/thao/venv/bin/python /home/thao/vodjs/job_tracking.py
StandardInput=tty-force
[Install]
WantedBy=multi-user.target
我按照这个教程https://websofttechs.com/tutorials/how-to-setup-python-script-autorun-in-ubuntu-18-04/
但是当我检查我的状态服务时它返回
Thg 12 15 11:42:50 thao-HP-ZBook-15 systemd[1]: Started Test Service.
Thg 12 15 11:42:50 thao-HP-ZBook-15 systemd[1]: job_tracking.service: Main process exited, code=exited, status=1/FAILURE
Thg 12 15 11:42:50 thao-HP-ZBook-15 systemd[1]: job_tracking.service: Failed with result 'exit-code'.
我做错了什么?编辑:/home/thao/logs
我在终端中运行时需要一个文件夹,但我不知道在 systemd 中运行时需要哪个文件夹。这是 log_app 函数:
import logging
from logging.handlers import RotatingFileHandler
log_formatter = logging.Formatter('%(asctime)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
logFile = './logs/app.log'
log_handler = logging.handlers.TimedRotatingFileHandler(logFile, when="D", interval=1, backupCount=0)
log_handler.setFormatter(log_formatter)
log_handler.setLevel(logging.INFO)
app_log = logging.getLogger('P2P')
app_log.setLevel(logging.INFO)
app_log.addHandler(log_handler)
def log_app(string):
#In ra bien string trong file log duoi LEVEL INFO
app_log.info(string)
print(string)