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

S A*_*rew 5 python ubuntu service 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
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

我查过了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.
Run Code Online (Sandbox Code Playgroud)

ale*_*ame 14

要查看记录的日志stdoutstderr可以使用journalctl 命令:

sudo journalctl -u [service_name]
Run Code Online (Sandbox Code Playgroud)


Cod*_*ers 8

sudo journalctl -u [service_name] -e 
Run Code Online (Sandbox Code Playgroud)

末尾的 -e 选项跳转到日志文件的末尾以查看最新的日志。