How to Use Anacron to Schedule Tasks on a Web Hosting

Gandi Web Hosting does not include the standard Unix/Linux scheduler, cron. However, it does have a powerful tool you can use for this purpose: anacron

Concept

The idea is straightforward: Decide what command or program (job) you want to run periodically, and at what interval. Put an entry in the configuration file to define the task and interval. The job will run at the interval defined.

Each job can have its own interval.

Note

We modified the standard version of anacron to make the minimum interval smaller and give our users more flexibility. The smallest interval you can set is now one hour.

Warning

Cron (and anacron) tasks take up PHP processes if your task utilizes PHP code. That means on a Size S, which has only 2 PHP processes that can run simultaneously, the cron job will take up one of these processes. You can lock up your web hosting if you run too many cron jobs at the same time, or at the same time as other concurrent processes. If you need a lot of cron jobs, consider a size M or larger web hosting.

Configuring

To configure anacron, you edit the anacrontab file. There is only one anacrontab file per web hosting, accessible via the Control Panel, SFTP and the Emergency Console.

To modify the file from the Control Panel, select “Edit scheduled tasks” under the “Cron” section.

SFTP path: /lamp0/etc/cron/anacrontab

Emergency Console path: /srv/data/etc/cron/anacrontab

If you choose the SFTP option, download the anacrontab file like you would any file you have access to on the web hosting. You can then edit it locally on your system, and when ready transfer your version back into the same directory. It will automatically be read and instantiated.

Note

Use any text editor that preserves the native line endings. Don’t use a word processor! Any line that begins with a # character is a comment. Comments are a good idea.

Determine the Frequency

You specify the frequency with two parameters, a frequency index and multiplier.

Frequency indices are (from smallest to largest):

  • Hourly (hour)

  • Daily (day)

  • Monthly (month)

  • Yearly (year)

To determine the frequency of your action, simply multiply the index by the desired value.

For example, if you want to perform an action every 2 hours, then enter: 2@hourly If you want an operation to take place each day as a backup of your database, for example, you can specify it in several ways. 24@hourly means every 24 hours. 1@daily means once a day. You can also write @daily without the multiplier, since 1 is assumed.

Syntax

Now that you know to determine the frequency, you need to know the syntax. There are four components:

  • Frequency

  • Timeout

  • Unique-name

  • Action

Timeout is not used in our version of anacron, and will be ignored. For compatibility, we do need to put in a value, however. Just use 0. The unique name is used only to identify the task to anacron. This name can be anything, and just has to be unique. Action is the command to run. You can enter a command line as if you were typing it in. You can even call scripts or programs you write.

In our example, we will set a purge of temporary files over 7 days old, run daily every day:

@daily 0 purgetmp find /src/data/tmp -type f -mtime +7 -delete >> /dev/null

@daily states that this action should take place once a day (we could say 1@daily, too). 0 corresponds to the timeout value that is ignored by the system. purgetmp is the unique name that we assigned to the action to identify it in the list. The command find /src/data/tmp -type f -mtime 7 -delete » /dev/null is used to delete temporary files over 7 days old.

Examples of Use

Exporting a MySQL Database

If you want to use snapshots to make copies of your web hosting, the MySQL database is stored in in a way that does not allow for a simple recovery of its data in the event of a problem.

To perform a backup of your databases, it is necessary to perform an export via the command mysqldump. In order to have backups that are up to date, you will need to create an anacron task to perform regular backups. Here is an example that you can add to your web hosting (with some necessary adaptation by you so it works with your web hosting):

@daily 0 mysql_backup mkdir -p /srv/data/tmp/mysql_backup ; mysqldump -u root {-pPASSWORD} --all-databases | /bin/gzip -9 > /srv/data/tmp/mysql_backup/`date '+%F'`.databases.sql.gz ; rm -f /srv/data/tmp/mysql_backup/`date '+%F' --date '1 week ago'`.databases.sql.gz

This line will cause your database to be exported every day, and will keep the last export for one week.

Tip

Consider adapting the command to your own configuration, if the user ‘root’ does not have a password, then you must delete {-pPASSWORD}, or delete the brackets {} and indicate the password of your ‘root’ user after ‘-p’.

Tip

You can execute the command directly via the console in order to verify that it works.

Exporting a PostgreSQL Database

The usage is similar to MySQL, the pg_dumpall command exports all postgresql databases on the web hosting.

@daily 0 psql_backup mkdir -p /srv/data/tmp/psql_backup ;  pg_dumpall --username=hosting-db -h localhost > /srv/data/tmp/psql_backup/`date '+%F'`.databases ; rm -f /srv/data/tmp/psql_backup/`date '+%F' --date '1 week ago'`.databases

Empty a Log File

You can use anacron to remove the contents of the log files from your web hosting, in the example below the Apache web server access.log file:

@daily 0 purge_apache_logs echo "***** Log cleaned at `date'+%F-%H%M'` *****" > /srv/data/var/log/apache/access.log

Executing a PHP Script

You can also execute scripts written in PHP. To do this, you will need to use the php command, along with the -f option, and the path to the file.

Your files will still be located at the same location (/srv/data/web/vhosts), followed by the directory of the VHOST and the path to the file within the code of your application.

In the following example, we execute a file called cron.php every day, which is at the root of our VHOST www.example.com.

@daily 0 my_php_script php -f /srv/data/web/vhosts/www.example.com/cron.php

FAQ

Why anacron instead of cron?

Anacron has the added benefit that if your web hosting is ever offline (full disk), when your web hosting is started again any tasks that were scheduled to run while the web hosting was offline will run. With cron those tasks would be skipped and you could miss out on potentially important backups tasks.

When will my anacron task run?

A service checks each hour to determine whether there are any tasks that are past-due and need to be run. The exact minute and second this happens is based on when the web hosting was started, plus a random delay to spread the load from multiple web hostings on a single node.