如何在Ubuntu中查看systemd服务日志

问题描述

我有一个下面的服务文件:

[Unit]
Description=demo service
Requisite=mosquitto.service
BindsTo=mosquitto.service

[Service]
Type=simple
ExecStart=/usr/bin/python3 /home/john/Documents/source/demo.py
Restart=on-failure
RestartSec=30s
StandardOutput=null

[Install]
WantedBy=multi-user.target

demo.service仅调用python代码demo.py。当我使用sudo systemctl start demo.service启动服务时,它始终处于auto-restart(failure)模式。

demo.service - demo service
   Loaded: loaded (/etc/systemd/system/demo.service; enabled; vendor preset: enabled)
   Active: activating (auto-restart) (Result: exit-code) since Mon 2020-08-17 12:13:37 BST; 23s ago
  Process: 4744 ExecStart=/usr/bin/python3 /home/john/Documents/source/demo.py
 Main PID: 4744 (code=exited,status=1/FAILURE)

我已经检查过syslog,但是其中没有有用的信息。它仅显示演示服务已启动,演示服务已停止日志。

我可以从哪里获取其日志,以了解为什么服务未启动。请帮忙。谢谢

下面是journactl的日志:

Aug 13 13:51:40 john systemd[1]: Started demo service.
Aug 13 13:51:40 john systemd[1]: demo.service: Main process exited,code=exited,status=1/FAILURE
Aug 13 13:51:40 john systemd[1]: demo.service: Failed with result 'exit-code'.
Aug 13 13:52:10 john systemd[1]: demo.service: Service hold-off time over,scheduling restart.
Aug 13 13:52:10 john systemd[1]: demo.service: Scheduled restart job,restart counter is at 2.
Aug 13 13:52:10 john systemd[1]: Stopped demo service.

解决方法

要查看已记录的stdoutstderr,可以使用journalctl命令:

sudo journalctl -u [service_name]

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...