大网数据广告活动
服务器运维问答

How do I tell why a systemd service was started?

· 52CE 我爱测

I am trying to figure out why a specific service is started on a system, and I wonder if there is a command to tell why a specific service was started? When asking for status on the service, systemctl claims that it is disabled, …

问题原文

原帖链接 · nafmo · CC BY-SA 3.0

I am trying to figure out why a specific service is started on a system, and I wonder if there is a command to tell why a specific service was started?

When asking for status on the service, systemctl claims that it is disabled, yet it is running and I haven't explicitly asked to start it, AFAICT. Output below (slightly anonymized)

# systemctl status myservice
● myservice.service - My Service
   Loaded: loaded (/usr/lib/systemd/system/myservice.service; disabled; vendor preset: disabled)
   Active: active (running) since mån 2017-02-27 13:57:15 CET; 30min ago
     Docs: http://www.example.com/
 Main PID: 4680 (ewe)
   CGroup: /system.slice/myservice.service
           ├─4680 /opt/myservice/vbc/bin/myservice
           └─4944 /opt/myservice/vbc/bin/myservice

I am starting a service that has set Before=myservice.service in its .service file (no Requires), so I am suspecting that, but I can't tell for sure.

This is running on a CentOS 7.3 system.

EDITED: I have been able to work around the issue by making sure that a service that the above service has a Requires=, After= and Wants= relationship to, and which is started by a transient run-once service, is explicitly stopped. When doing this, the service is not started. I am not closer to figuring out why it was started in the first place, however.

EDITED: It seems that my service file is started whenever one of the services it has a Requires relationship to is restarted. I did not expect that to happen, I assumed that it would only mean that my service would start the other service when started, not that it would also start mine. Removing Requires fixes the phantom restarts.

社区高分解答

原帖链接 · iwaseatenbyagrue · CC BY-SA 3.0

Running sudo systemctl status might help, as might systemctl list-dependencies.

I had heard of services being started on install on (I think) Ubuntu, but never on CentOS. However, IF your service has a socket unit defined, then traffic to a given socket/port could be causing your service to start automatically.

I have such a setup for TFTP, for example:

sudo systemctl list-dependencies | grep socket

│ ├─sockets.target
│ │ ├─dbus.socket
<snip>
│ │ └─tftp.socket

Config for it says to start the service if UDP/69 traffic is received:

# /usr/lib/systemd/system/tftp.socket
[Unit]
Description=Tftp Server Activation Socket

[Socket]
ListenDatagram=69

[Install]
WantedBy=sockets.target

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-02-27)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。