我对使用 systemd 计时器相当陌生,并且遇到了一些问题。
我正在尝试安排一个脚本每天运行一次,每 8 小时运行一次,时间为上午 6 点、下午 2 点和晚上 10 点。时间开始正确,并且显示下一个计划运行时间(确实如此),但它似乎永远不会运行第三次(或任何其他)时间。我究竟做错了什么?
我的计时器中有这个:
[Unit]
Description=Run every 8 hours
Requires=script.service
[Timer]
OnCalendar=*-*-* 03,11,19:00:00
Persistent=true
[Install]
WantedBy=timers.target
我也尝试过这个:
[Unit]
Description=Run every 8 hours
Requires=script.service
[Timer]
OnCalendar=*-*-* 03,11,19:00:00
OnUnitActiveSec=1d
Persistent=true
[Install]
WantedBy=timers.target
和这个:
[Unit]
Description=Run every 8 hours
Requires=script.service
[Timer]
OnCalendar=*-*-* 03:00:00
OnCalendar=*-*-* 11:00:00
OnCalendar=*-*-* 19:00:00
Persistent=true
[Install]
WantedBy=timers.target
服务:
[Unit]
Description=Renews Kerberos ticket every 8 hours
After=network-online.target firewalld.service
Wants=network-online.target script.timer
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStartPre=/usr/bin/kdestroy
ExecStart=/usr/bin/kinit -R -V [email protected] -k -t /etc/krb5.keytab
IOSchedulingClass=best-effort
[Install]
WantedBy=default.target
'''
答案1
好的,我相信问题出在脚本服务文件。根据systemd.timer
手册页:
DESCRIPTION
Note that in case the unit to activate is already active at the time the timer elapses it is not restarted, but simply left running. There is no concept of spawning new service instances in this case. Due to this, services
with RemainAfterExit= set (which stay around continuously even after the service's main process exited) are usually not suitable for activation via repetitive timers, as they will only be activated once, and then stay
around forever.
去掉RemainAfterExit=
线,你就可以开始了。