如何让 Anacron 可靠地启动?

如何让 Anacron 可靠地启动?

因此,我使用 Anacron 来每天可靠地运行一些脚本。但是,当 Anacron 本身有一半时间无法在启动时启动时,这种方法就行不通了。我该如何让 Anacron 可靠地启动?

> grep 'anacron' /var/log/syslog.2
May 18 19:09:02 s-laptop anacron[2480]: Job `cron.daily' terminated (exit status: 1) (mailing output)
May 18 19:09:02 s-laptop anacron[2480]: Can't find sendmail at /usr/sbin/sendmail, not mailing output
May 18 19:09:02 s-laptop anacron[2480]: anacron: Can't find sendmail at /usr/sbin/sendmail, not mailing output
May 18 19:09:02 s-laptop anacron[2480]: Normal exit (1 job run)
May 18 21:20:48 s-laptop systemd[1]: Started Run anacron jobs at resume.
May 20 16:30:46 s-laptop systemd[1]: Started Run anacron jobs at resume.
May 20 17:02:27 s-laptop systemd[1]: Started Run anacron jobs at resume.
May 20 18:58:50 s-laptop systemd[1]: Started Run anacron jobs at resume.
May 20 19:13:48 s-laptop systemd[1]: Started Run anacron jobs.
May 20 19:13:48 s-laptop anacron[734]: Anacron 2.3 started on 2017-05-20
May 20 19:13:48 s-laptop anacron[734]: Will run job `cron.daily' in 5 min.
May 20 19:13:48 s-laptop anacron[734]: Will run job `cron.weekly' in 10 min.
May 20 19:13:48 s-laptop anacron[734]: Jobs will be executed sequentially
May 20 19:18:51 s-laptop anacron[734]: Job `cron.daily' started
May 20 19:18:51 s-laptop anacron[2367]: Updated timestamp for job `cron.daily' to 2017-05-20
> grep 'anacron' /var/log/syslog.1
May 20 19:18:54 s-laptop anacron[734]: Job `cron.daily' terminated (exit status: 1) (mailing output)
May 20 19:23:48 s-laptop anacron[734]: Job `cron.weekly' started
May 20 19:23:48 s-laptop anacron[2606]: Updated timestamp for job `cron.weekly' to 2017-05-20
May 20 19:30:09 s-laptop anacron[734]: Job `cron.weekly' terminated
May 20 19:30:09 s-laptop anacron[734]: Normal exit (2 jobs run)
May 21 10:02:56 s-laptop systemd[1]: Started Run anacron jobs at resume.
May 25 12:53:39 s-laptop systemd[1]: Started Run anacron jobs at resume.
Jun  1 18:09:14 s-laptop systemd[1]: Started Run anacron jobs at resume.
Jun  3 12:29:40 s-laptop anacron[751]: Anacron 2.3 started on 2017-06-03
Jun  3 12:29:40 s-laptop anacron[751]: Will run job `cron.daily' in 5 min.
Jun  3 12:29:40 s-laptop anacron[751]: Will run job `cron.weekly' in 10 min.
Jun  3 12:29:40 s-laptop systemd[1]: Started Run anacron jobs.
Jun  3 12:29:40 s-laptop anacron[751]: Will run job `cron.monthly' in 15 min.
Jun  3 12:29:40 s-laptop anacron[751]: Jobs will be executed sequentially
Jun  3 12:34:40 s-laptop anacron[751]: Job `cron.daily' started
Jun  3 12:34:40 s-laptop anacron[2243]: Updated timestamp for job `cron.daily' to 2017-06-03
> grep 'anacron' /var/log/syslog
Jun  3 12:34:44 s-laptop anacron[751]: Job `cron.daily' terminated (mailing output)

从系统日志中可以看出,尽管机器分别于 5 月 21 日、25 日和 6 月 1 日启动,但 Anacron 仅在 5 月 20 日和 6 月 3 日启动时启动。

此外,今天(6 月 3 日),我实际上是在 12:11 启动了计算机,看到 Anacron 尚未启动,而且我的作业均未运行,因此我在 12:29 重新启动了计算机。您可以看到,在第二次启动时,Anacron 决定启动。为什么它很多时候都无法启动?

答案1

编辑:这个问题似乎已在 Debian 10 和 Ubuntu 19.04 中得到修复。

因此,如果使用电池供电,Anacron 似乎不会在启动时运行,否则只会在早上 7:30 运行。对我来说,解决方案是将 Anacron 配置为在插入电源时运行。

可以通过添加如下脚本来实现:

#!/bin/sh
test -x /etc/init.d/anacron && /usr/sbin/invoke-rc.d anacron start >/dev/null

我将其保存到/etc/pm/power.d/10_anacron(确保它是可执行的),在较旧的系统上,插入时会自动运行。在当前版本的 Ubuntu 上,您需要添加 udev 规则来运行该脚本。只需保存:

SUBSYSTEM=="power_supply", ATTR{online}=="1", RUN+="/etc/pm/power.d/10_anacron"

/etc/udev/rules.d/anacron.rules

答案2

一种更简单的方法:只需将文件中的一行更改/etc/default/anacron为:

ANACRON_RUN_ON_BATTERY_POWER=yes

相关内容