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

Systemd timer not starting its service unit

· 52CE 我爱测

Situation I have written a custom systemd service unit and its companion shell script to renew a certificate from Let's Encrypt . Everything works fine when I run systemctl start letsencrypt-example_com.service . I want it to be …

问题原文

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

Situation

I have written a custom systemd service unit and its companion shell script to renew a certificate from Let's Encrypt. Everything works fine when I run systemctl start letsencrypt-example_com.service. I want it to be run automatically every 60 days, so I wrote a systemd timer unit.

Issue

I ran systemctl enable letsencrypt-example_com.timer then systemctl start letsencrypt-example_com.timer. The timer seems to start but not the service.

# systemctl status letsencrypt-example_com.timer
Created symlink from /etc/systemd/system/timers.target.wants/letsencrypt-example_com.timer to /etc/systemd/system/letsencrypt-example_com.timer.
# systemctl start letsencrypt-example_com.timer
# systemctl list-timers --all
# systemctl list-timers
NEXT                           LEFT     LAST                           PASSED       UNIT                            ACTIVATES
n/a                            n/a      ven. 2016-05-06 13:10:13 CEST  1h 51min ago letsencrypt-example_com.timer letsencrypt-example_com.service
# systemctl status letsencrypt-example_com.timer
● letsencrypt-example_com.timer - Run letsencrypt-example_com every 60 days
   Loaded: loaded (/etc/systemd/system/letsencrypt-example_com.timer; enabled)
   Active: active (elapsed) since ven. 2016-05-06 15:01:57 CEST; 2min 50s ago
# systemctl status letsencrypt-example_com.service
● letsencrypt-example_com.service - letsencrypt certificat renewal for example.com and subdomains
   Loaded: loaded (/etc/systemd/system/letsencrypt-example_com.service; static)
   Active: inactive (dead)

Files

cat /etc/systemd/system/letsencrypt-example_com.service :

[Unit]
Description=letsencrypt certificat renewal for example.com and subdomains
Requires=nginx_reload.service
Before=nginx_reload.service

[Service]
Type=simple
ExecStart=/bin/sh /usr/local/bin/letsencrypt-renew.sh example.com www.example.com
User=letsencrypt
Group=www-data

/usr/local/bin/letsencrypt-renew.sh :

#!/bin/sh

letsencrypt certonly \
--server https://acme-v01.api.letsencrypt.org/directory \
--text \
--email [email omitted] \
--agree-tos \
--rsa-key-size 4096 \
--authenticator webroot \
--webroot-path /srv/files/letsencrypt/www \
$(
for fqdn in $@;
    do echo "--domain $fqdn";
    done;
) \
--force-renew

/etc/systemd/system/letsencrypt-example_com.timer :

[Unit]
Description=Run letsencrypt-example_com every 60 days

[Timer]
OnUnitActiveSec=1min
Persistent=true
Unit=letsencrypt-example_com.service

[Install]
WantedBy=timers.target

/etc/systemd/system/nginx_reload.service :

[Unit]
Description=reload nginx conf

[Service]
Type=oneshot
ExecStart=/bin/systemctl reload nginx

已采纳解答

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

Although timer units automatically gain a Before= dependency on the service they are supposed to activate, they apparently don't automatically have a Requires= dependency on it (which doesn't make any sense to me).

So I added the following line to the [Unit] section of the timer unit, and now it starts the service as intended :

Requires=letsencrypt-example_com.service

I also set an AccuracySec (of 10s) in the [Timer] section.

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

资料来源与适用范围

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

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

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