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

How is a systemd service unit activated by a timer unit?

· 52CE 我爱测

Given a unit file foo.service # /etc/systemd/system/foo.service [Service] ExecStart=/opt/bin/foo --prod And a unit file foo.timer # /etc/systemd/system/foo.timer [Timer] OnCalendar=*-*-* 12:00:00 [Install] WantedBy=timers.target …

问题原文

原帖链接 · mbigras · CC BY-SA 4.0

Given a unit file foo.service

# /etc/systemd/system/foo.service
[Service]
ExecStart=/opt/bin/foo --prod

And a unit file foo.timer

# /etc/systemd/system/foo.timer
[Timer]
OnCalendar=*-*-* 12:00:00

[Install]
WantedBy=timers.target
  • What happens at 12:00 every day if foo.service is off? (it will probably be started)
  • What happens at 12:00 every day if foo.service is already on? (will it be restarted?)

From reading man systemd.timer

For each timer file, a matching unit file must exist, describing the unit to activate when the timer elapses. By default, a service by the same name as the timer (except for the suffix) is activated. Example: a timer file foo.timer activates a matching service foo.service. The unit to activate may be controlled by Unit= (see below).

  • What does it mean to activate foo.service? Does it mean restart or verify that it's already started?

已采纳解答

原帖链接 · Thomas · CC BY-SA 4.0

Normally the systemd service of a timer is a oneshot or simple (default) start-up type, which means the service runs a command and exits afterwards.
During the execution of the command given with ExecStart, the unit has the state active (running) and on exit something like inactive (dead).

When using timers you do not activate the service with systemctl enable --now foo.service but rather enable the corresponding timer unit systemctl enbale --now foo.timer.
What the timer does is more or less a systemctl start foo.service at the time given.


  • What happens at 12:00 every day if foo.service is off? (it will probably be started)

The foo.service will be started but is not enabled (auto-start at boot).

  • What happens at 12:00 every day if foo.service is already on? (will it be restarted?)

If the service is in an active (running) state nothing will happen. systemd won't try to start a service if that is already running.

  • What does it mean to activate foo.service? Does it mean restart or verify that it's already started?

To activate a service means to start it, e.g. systemctl start foo.service.

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

资料来源与适用范围

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

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

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