Selects logo a praymentis.

I AM SELECT

Dec 09
2020
If you want to run script in regular intervals, like daily or weekly, on a computer that is not always running, use `anacron`. It runs tasks when you start your computer.

Anacron - automate a script to run every day on your laptop

A problem that you face at some point spending enough time with your computer, is running a regular backup, from your computer to a server or from a server to your computer. While there are ready made solutions out there like Dejadub, they are not always compatible with what you are trying to achieve or they are just to bloated for a simple backup task.

For repetitive tasks Linux provides a well known program call cron. However cron is not an option for a laptop that you only boot once a week wen you want to run the backup every week. Why? Because you can only configure cron to run the task let's say on Monday, so if you boot your laptop on Tuesday cron will have missed to run the task. This is where anacron comes in.

anarcon runs scripts for you when you start your computer. You can specify how often they should be run (daily / weekly / monthly) and anacron keeps timestamps of each tasks last run to check if they need to be executed again.

Setup

First of all you will need to run anarcron when you start up your computer. There are several methods for auto starting a script on login. 2 Examples:

  • Add the following command to 📄~/.xprofile if you are using X11
  • On Ubuntu (Gnome) run gnome-session-properties to set an auto start command.

This is the full anacron command you need to run (replace foo with your username).

anacron -s -t /home/foo/.anacron/anacrontab -S /home/foo/.anacron/timestamps

The command uses the config file 📄anacrontab and stores the timestamps from the last run in the folder 📁timestamps. (Your can choose the names freely, this is just my naming.)

Next you will have to create the directories needed for anacron and the anacontab file in your home directory

mkdir -p ~/.anacron/{timestamps,cron.daily}
touch ~/.anacron/anacrontab

The content of the anacrontab file should look something like this:

📄~/.anacron/anacrontab
# See anacron(8) and anacrontab(5) for details.
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# period delay  job-identifier command
1        15     cron.daily     run-parts -v /home/foo/.anacron/cron.daily

The SHELL and PATH variables are needed to run your scripts in the right shell with all programs available. In the last line all script files located at 📁/home/foo/.anacron/cron.daily are executed by using run-parts, which executes all scripts in a directory and output the script name to stderr -v. The scripts should be run 15 minutes after you logged in and they should run once a day (1 in the example, whereas 3 would mean every 3 days).

To add script that should be run on this interval, place or soft link them in the cron.daily directory.

ln -s /source/path/script /home/foo/.anacron/cron.daily/script

Testing the setup

To test if everything worked use the -d flag to keep anacron running in the foreground.

anacron -s -t /home/foo/.anacron/anacrontab -S /home/foo/.anacron/timestamps -d

Other time intervals

To run scripts monthly or weekly create the according cron.* directories and add the following to your anacrontab file (@monthly is used since month have different number of days).

📄~/.anacron/anacrontab
7          15  cron.weekly   run-parts -v /home/foo/.anacron/cron.weekly
@monthly   20  cron.monthly  run-parts -v /home/foo/.anacron/cron.monthly

Caveat

Since anacron only runs when your computer starts, this means that if you leave your computer running for more than a day the scripts will only be executed once. If this is a problem you will have to work with cron.

Sauce