2

So, I'm using Anacron to be able to reliably run some scripts daily. However, this doesn't work when Anacron itself doesn't start on boot half the time. How would I get Anacron to start up reliably?

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

As can be seen in the syslog, Anacron was only started at boot on May 20th and June 3rd, despite the machine being booted May 21st and 25th and June 1st.

Additionally, today (June 3rd), I actually booted my computer at 12:11, and seeing that Anacron had not started and none of my jobs were run, I rebooted the machine at 12:29. You can see on this second boot, Anacron decided to startup. Why does it just not startup a lot of the time?

  • Anacron does not have a just-start-sometimes-at-random feature. After a couple reboots, my Anacron starts quite reliably on my test system. Can you suggest any changes I can make to my test system that would enable me to duplicate your problem? – user535733 Jun 3 '17 at 15:48
  • OK, just started looking at the init scripts, and I think I see why. Anacron must not have started because I was on battery power. Looking at the cron entries for anacron, the next time anacron will be run is 7:30am. Is this correct? It seems a bit odd that something that is meant to make cron more reliable, will completely miss everything if it's not plugged in at boot and switched off at 7:30am. So, is it an acceptable solution to change that cron entry to run every hour, or will this cause any issues? Or, better yet, run anacron every time the machine is plugged in? – Sam Bull Jun 3 '17 at 18:07
  • Hang on. Anacron is NOT an always-on (daemon/service). It's a tiny little shim of a program that runs at startup/resume, checks the timestamp files to see if any cron jobs were missed, runs them if needed, and exits. Your logs seem to indicate that Anacron was running, and simply decided that the jobs didn't need to be triggered. Before going deep into debugging Anacron, compare first the scheduled vs actual times for cron.daily and cron.weekly. The answer might be as simple as moving your cron.daily schedule to earlier in the day. – user535733 Jun 3 '17 at 21:34
  • There's no anacron started message, and nothing about running cron.daily for 05/21, 05/25, 06/01 or the first time I booted my computer 06/03 at 12:11. It did not run my cron.daily scripts anytime between 05/13 and 06/03. Despite my computer having been turned on a number of times and being switched on for several hours. It looks to me like the init script is responsible for running Anacron on boot, but won't run it if on battery (/etc/init.d/anacron). And the only other time I can see it getting run is at 7:30am in /etc/cron.d/anacron. – Sam Bull Jun 4 '17 at 13:43
  • I've also just tried adding a udev rule to run anacron when plugged in. This seems to work correctly, so I think this might be the correct fix. – Sam Bull Jun 4 '17 at 14:20
4

Edit: This appears to have been fixed upstream in Debian 10 and Ubuntu 19.04.

So, it seems that Anacron doesn't run at startup if on battery power, and is only scheduled to run at 7:30am otherwise. The solution for me was to configure Anacron to run when power is plugged in.

This can be done by adding a script like this:

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

I saved it into /etc/pm/power.d/10_anacron (make sure it is executable), which on older systems would be run automatically when plugged in. On current versions of Ubuntu, you need to add a udev rule to run the script. Just save:

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

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

1

An easier way: just change a line in the file /etc/default/anacron to:

ANACRON_RUN_ON_BATTERY_POWER=yes
  • Which would cause it to run on battery and waste power, exactly why it is set to no in the first place. It depends whether reliability or battery is a bigger issue for you. The solution I'm using has no impact on battery life, but significantly increases the reliability from the default setup. – Sam Bull Aug 28 '17 at 11:04
  • Ah, now I got your problem! In that case, your solution is the better one. But maybe you should edit your question so that people can immediately see that it's only about running anacron when the laptop is plugged in. – mzuther Aug 29 '17 at 13:36
  • Well, the problem was that it almost wasn't running at all. Through further digging, I figured out the issue was caused by it only running on boot (and skipping if on battery). So, my solution means that it now runs most days I use my laptop, while before it was barely running once a month. – Sam Bull Aug 29 '17 at 17:44
  • This change doesn't works on Ubuntu 16.04 – NicolasSmith Oct 26 '17 at 6:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.