我希望使用 systemd 每 85 天运行一次命令。我为该命令创建了一个计时器和服务,但我无法启动该计时器。我希望让该命令每 85 天运行一次,而不是在启动时运行,并且即使在 85 天期间服务器重新启动也能运行。
/etc/systemd/system/my.service
[Unit]
description=Do my task
[Service]
Type=oneshot
ExecStart=/usr/local/sbin/myTask
/etc/systemd/system/my.timer
[Unit]
Description=Timer to do my task
[Timer]
OnUnitActiveSec=85d
Unit=my.service
Persistent=true
尝试使用启动它后systemctl start my.timer
,我收到的错误消息是:
my.timer: Refusing to start, unit to trigger not loaded.
Failed to start Timer to do my task.
答案1
description
将服务文件中的更改为Description
。否则,它将在日志中打印如下错误:
‘单位’部分中的未知左值‘描述’
另外,尝试使用这些命令来解决计时器文件的问题:
systemctl status my.timer
journalctl -xe
如果您在日志中发现任何错误并且不知道如何修复它们,请更新您的帖子。
答案2
问题是我尝试运行的脚本在开始时没有爆炸声。
我的诊断是:
- 看看 Andril 上面提到的时间状态
# systemctl status my.timer
● my.timer - Timer to update stuff
Loaded: loaded (/etc/systemd/system/my.timer; enabled; vendor preset: enabled)
Active: active (elapsed) since Tue 2020-01-28 07:14:54 UTC; 2 weeks 6 days ago
Trigger: n/a
Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.
- 查看计时器列表(以获取更多统计数据)
# systemctl list-timers
NEXT LEFT LAST PASSED UNIT ACTIVATES
...
n/a n/a Tue 2018-03-20 14:13:46 UTC 1 years 10 months ago my.timer my.service
- 尝试运行该服务以激活该服务(因为计时器基于上次激活该服务的时间
# systemctl status my.service
● my.service - Renew Lets Encrypt certificates
Loaded: loaded (/etc/systemd/system/my.service; static; vendor preset: enabled)
Active: failed (Result: exit-code) since Mon 2020-02-17 15:53:18 UTC; 20s ago
Process: 19875 ExecStart=/usr/local/sbin/script (code=exited, status=203/EXEC)
Main PID: 19875 (code=exited, status=203/EXEC)
Feb 17 15:53:18 server systemd[1]: Starting Update stuff...
Feb 17 15:53:18 server systemd[19875]: my.service: Failed to execute command: Exec format error
Feb 17 15:53:18 server systemd[19875]: my.service: Failed at step EXEC spawning /usr/local/sbin/script: Exec format error
Feb 17 15:53:18 server systemd[1]: my.service: Main process exited, code=exited, status=203/EXEC
Feb 17 15:53:18 server systemd[1]: my.service: Failed with result 'exit-code'.
Feb 17 15:53:18 server systemd[1]: Failed to start Update stuff.
- 通过反复试验,我查看了剧本,发现它没有爆炸声,所以加了一个
# vim /usr/local/sbin/script
#!/bin/bash
echo Update!
- 尝试再次运行该服务
# systemctl start my.service
# systemctl status letsencrypt.service
● my.service - Update stuff
Loaded: loaded (/etc/systemd/system/my.service; static; vendor preset: enabled)
Active: inactive (dead) since Mon 2020-02-17 16:05:45 UTC; 4s ago
Process: 31781 ExecStart=/usr/local/sbin/script (code=exited, status=0/SUCCESS)
Main PID: 31781 (code=exited, status=0/SUCCESS)
Feb 17 16:05:45 server systemd[1]: Starting Update stuff...
Feb 17 16:05:45 server test[31781]: Update!
Feb 17 16:05:45 server systemd[1]: Started Update stuff.
(哇噢)
- 再次检查了计时器
# systemctl list-timers
NEXT LEFT LAST PASSED UNIT ACTIVATES
...
Tue 2020-05-12 16:05:45 UTC 2 months 24 days left Tue 2018-03-20 14:13:46 UTC 1 years 10 months ago my.timer my.service
x timers listed.
Pass --all to see loaded but inactive timers, too.
(现在计时器显示它将再次运行)