4

I have a clean install of Xubuntu 14.04 32-bit, and anacron doesn't seem to be working.

I read that it's better to use a cron job to trim SSDs, and that Ubuntu has a cron job to do that, so I removed discard from /etc/fstab. I wanted to confirm that the cron job was working, so I added an echo command to /etc/cron.weekly/fstrim so that it looks like this:

#!/bin/sh
# call fstrim-all to trim all mounted file systems which support it
echo "Trim started on" $(date) >> /home/dominic/Desktop/Trim_Runs
set -e
# This only runs on Intel and Samsung SSDs by default, as some SSDs with faulty
# firmware may encounter data loss problems when running fstrim under high I/O
# load (e. g.  https://launchpad.net/bugs/1259829). You can append the
# --no-model-check option here to disable the vendor check and run fstrim on
# all SSD drives.
exec fstrim-all

It runs fine from a terminal, but it never runs as a weekly job. So I moved it to cron.daily, but it never runs from there either. So I moved it to cron.hourly, and it does run every hour. The echo text appears in the file, and the drive light comes on for about two minutes. But cron.hourly does not use anacron.

Here is my crontab file. I have changed some of the times, but it didn't seem to work with the original times that came with Xubuntu either.

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 16   * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report      /etc/cron.daily )
47 6    * * 1   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#

I tested to see if it would run correctly from my user's crontab by putting the following into crontab -e. Then I waited a few minutes until 8:10 pm, but nothing happened.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

10 20   * * *   test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )

I think it must have the correct syntax for run-parts, because it does run when I move the script into cron.hourly.

It seems that cron is working but anacron is not. So my question is, what can I do to get anacron working?

Zanna
  • 69,223
  • 56
  • 216
  • 327
Dominic
  • 51
  • 4
  • 3
    Could you show us the relevant cron lines? There may be a syntax error. Also, `cron` (not sure about `anacron`) uses `run-parts` to run the scripts in `/etc/cron.weekly` and `run-parts` has some very strict requirements for script naming. Have a look at `man run-parts` for details (though it does look like that script should run OK). Also test whether it runs correctly from your own user's crontab (`crontab -e`). – terdon May 20 '14 at 01:14
  • 1
    Hi terdon - thanks for the response. I have added the information you asked for to the original question. I hope that was the right thing to do. I'm new here. – Dominic May 20 '14 at 02:31
  • That is _exactly_ the right thing to do, thanks :). Whenever people ask for info in the comments, you should add the info to the question body and then reply (you can ping users with a `@`, for example to ping me just include `@terdon` in your comment) to the user who asked for it. Precisely as you did. I was hoping there was a syntax error but everything looks fine. I don't have any experience with anacron though. Why don't you add the command you want to run to your user's crontab? `* * * * * /path/to/fstrim`. That will launch it every minute and you can at least check that it works. – terdon May 20 '14 at 02:38
  • Why are you adding another script for trim? It is run automatically by ubuntu without the need of you fiddling with it. – Braiam May 20 '14 at 02:49
  • Hi @Braiam. I didn't add another script. I just added an echo command to the existing script that came with Xubuntu. Then terdon suggested that I try crontab -e, but that didn't work either. – Dominic May 20 '14 at 02:59

1 Answers1

1

I got anacron working on my system, as follows:

In /etc/fstab I have some directories moved to tmpfs to avoid writes to the SSD, like this:

tmpfs   /tmp       tmpfs   nodev,nosuid,noatime,mode=1777   0  0
tmpfs   /var/spool tmpfs   nodev,nosuid,noatime,mode=1777   0  0
tmpfs   /var/log   tmpfs   nodev,nosuid,noatime,mode=0755   0  0

This means that /var/spool/anacron/ does not exist at boot time, and as a result, anacron does not function. The three files in this directory must be preserved across system boots for anacron to work. So I created a directory /usr/local/etc/anacron/, and at boot time I create /var/spool/ and put a symlink in it that points to that directory. Now anacron is working because its three files (cron.daily, cron.weekly, and cron.monthly) are preserved across boots.

Actually, I create a bunch of directories at boot, as described here: How to fix anacron & cups-pdf when you have /var on tempfs but I modified the script so that it doesn't create /var/spool/anacron but instead it creates the symlink mentioned above.

The resulting script looks like this:

#!/bin/bash

# Script to create required directories in tempfs /var/log (that are not otherwise created).
# This script is needed because I have some directories moved to tmpfs in /etc/fstab.
# That means these directories disappear every time I shut down.
# Programs should create them if they do not exist, but some, like anacron, fail to do   so, and then fail to run.
# So I create them here. I don't know where it gets the permissions from, or whether they are right.
# Thanks to http://blog.philippklaus.de/2011/02/ssd-optimizations-on-linux/ for the list below :-)

for dir in apparmor apt ConsoleKit cups dist-upgrade fsck gdm hp installer lightdm news ntpstats samba speech-dispatcher unattended-upgrades upstart; do
  if [ ! -d /var/log/$dir ] ; then
    mkdir /var/log/$dir
  fi
done

# And in /var/spool.
for dir in cups-pdf; do
  if [ ! -d /var/spool/$dir ] ; then
    mkdir /var/spool/$dir
  fi
done

# Create the symlink.
ln -s /usr/local/etc/anacron /var/spool/anacron

The above script is in my home directory and is run at boot time by a command in /etc/rc.local as described in the linked article.

Maybe the real solution to this problem would be for anacron to store cron.daily, cron.weekly, and cron.monthly in a directory that the user is not likely to move to tmpfs.

Dominic
  • 51
  • 4