系统计时器未启动其服务单元


9

情况

我已经编写了一个定制的systemd服务单元及其配套的shell脚本,以从Let's Encrypt续订证书。我跑步时一切正常systemctl start letsencrypt-example_com.service。我希望它每60天自动运行一次,所以我编写了一个systemd计时器单元。

问题

systemctl enable letsencrypt-example_com.timer然后我跑了systemctl start letsencrypt-example_com.timer。计时器似乎已启动,但服务未启动。

# 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)

档案

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 admin@example.com \
--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

如果我今天做过,那么我可能还没有创建一个nginx_reload.service,并将其添加到letsencrypt-example_com.service insteadPermissionsStartOnly=true ExecStartPost=/bin/systemctl reload nginx
pandark

Answers:


11

尽管计时器单元自动获得了Before=对它们本应激活的服务的依赖,但显然它们并不自动对其具有Requires=依赖关系(这对我来说没有任何意义)。

因此,我[Unit]在计时器单元的部分中添加了以下行,现在它按预期启动了服务:

Requires=letsencrypt-example_com.service

我还在本节中设置了AccuracySec(的10s[Timer]


我前段时间有一个工作计时器,然后同一个计时器因相同的错误而停止;我解决了使用此解决方案;我认为在systemd升级中有些改变...
Zac
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.