Situasi
Saya telah menulis unit layanan systemd khusus dan skrip shell pengiringnya untuk memperbarui sertifikat dari Let's Encrypt . Semuanya bekerja dengan baik ketika saya berlari systemctl start letsencrypt-example_com.service
. Saya ingin dijalankan secara otomatis setiap 60 hari, jadi saya menulis unit pengatur waktu systemd.
Isu
Saya berlari systemctl enable letsencrypt-example_com.timer
kemudian systemctl start letsencrypt-example_com.timer
. Timer tampaknya mulai tetapi tidak layanan.
# 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)
File
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 protected] \
--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
, dan menambahkan ini keletsencrypt-example_com.service instead
:PermissionsStartOnly=true
ExecStartPost=/bin/systemctl reload nginx
Jawaban:
Meskipun unit pengatur waktu secara otomatis mendapatkan
Before=
ketergantungan pada layanan yang seharusnya mereka aktifkan , mereka tampaknya tidak secara otomatisRequires=
bergantung pada layanan tersebut (yang tidak masuk akal bagi saya).Jadi saya menambahkan baris berikut ke
[Unit]
bagian unit pengatur waktu, dan sekarang ia memulai layanan sebagaimana dimaksud:Saya juga menetapkan
AccuracySec
(dari10s
) di[Timer]
bagian.sumber