systemd 服务名称和日志条目之间的关系

systemd 服务名称和日志条目之间的关系

示例 [来自 Ubuntu 17.04]

该服务称为network-managersystemd。

$ systemctl list-unit-files | grep network
network-manager.service                    enabled  

查找相关日志信息会给人一种没有条目的印象。

$ sudo journalctl -u network-manager
-- No entries --
$ sudo journalctl -u network-manager.service
-- No entries --

最终我发现它是:

$ sudo journalctl -u NetworkManager

是否有一致的方法来了解服务以什么名称记录在journald日志中?

答案1

系统上同时拥有NetworkManager和服务会产生混乱:network-manager

$ systemctl list-unit-files | grep NetworkManager
NetworkManager-dispatcher.service          enabled  
NetworkManager-wait-online.service         enabled  
NetworkManager.service                     enabled  
$ systemctl list-unit-files | grep network-manager
network-manager.service                    enabled 

journalctl -u对于没有日志条目和不存在的单元,以相同的方式响应:

$ sudo journalctl -u network-manager
-- No entries --
$ sudo journalctl -u there-sure-isnt-a-service-named-like-this
-- No entries --

有一种更简单的方法来获取属于某个服务的日志文件,那就是使用systemctl status.

$ sudo systemctl status network-manager.service 
● NetworkManager.service - Network Manager
   Loaded: loaded (/lib/systemd/system/NetworkManager.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2017-08-15 16:12:15 CEST; 2 days ago
     Docs: man:NetworkManager(8)
 Main PID: 12950 (NetworkManager)
    Tasks: 4 (limit: 4915)
   CGroup: /system.slice/NetworkManager.service
           ├─12950 /usr/sbin/NetworkManager --no-daemon
           └─13011 /sbin/dhclient -d -q -sf /usr/lib/NetworkManager/nm-dhcp-helper -pf /var/run/dhclient-wlp4s0.pid -lf /var/lib/NetworkManager/dhclient-c19d95a3-6082-4c89-ab

Aug 17 15:09:48 alan-N551JM NetworkManager[12950]: <info>  [1502975388.9172] device (wlp4s0): supplicant interface state: associating -> 4-way handshake
Aug 17 15:09:48 alan-N551JM NetworkManager[12950]: <info>  [1502975388.9386] device (wlp4s0): supplicant interface state: 4-way handshake -> completed
Aug 17 15:11:48 alan-N551JM NetworkManager[12950]: <info>  [1502975508.7640] device (wlp4s0): supplicant interface state: completed -> authenticating
Aug 17 15:11:48 alan-N551JM NetworkManager[12950]: <info>  [1502975508.7707] device (wlp4s0): supplicant interface state: authenticating -> associating
Aug 17 15:11:48 alan-N551JM NetworkManager[12950]: <info>  [1502975508.7876] device (wlp4s0): supplicant interface state: associating -> 4-way handshake
Aug 17 15:11:48 alan-N551JM NetworkManager[12950]: <info>  [1502975508.8064] device (wlp4s0): supplicant interface state: 4-way handshake -> completed

这为您提供了服务名称的自动完成选项以及非现有服务的错误:

$ sudo systemctl status there-sure-isnt-a-service-named-like-this
Unit there-sure-isnt-a-service-named-like-this.service could not be found.

NetworkManager注意:我确信和之间存在联系network-manager,但这对答案并不重要。

答案2

服务的确切名称可以在以下位置找到:

/etc/systemd/system/*
/usr/lib/systemd/system/*

相关内容