Setting up scheduled tasks in Simple Hosting (cron)

Gandi Simple Hosting does not include the standard Unix/Linux scheduler, cron. It does have a powerful tool you can use for this purpose, however: anacron
This article shows you how to set up scheduled tasks with anacron in Gandi Simple Hosting.


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 time and interval defined.

Each job can have its own interval.

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

Warning! Cron (and anacron) tasks take up processes. That means on a Size S, which has only 2 processes that can run simultaneously, the cron job will take up one of these processes. You can lock up your instance 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 instance.

How to set up anacron

To configure anacron, you edit the anacrontab file. There is only one anacrontab per Simple Hosting instance. It is located here:


Just download this file with SFTP, like you would any file you have access to on the instance. You can then edit it locally on your system, and when ready transfer your version back into place. It will automatically be read and instantiated.

Editing the anacrontab file

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 (it can also be written as “@daily” without the multiplier, since 1 is assumed)


Now that you know to determine the frequency, you just 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 is 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

In the event that you want to use snapshots to make backups of your instance, 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 in order to perform regular backups. Here is an example that you can add to your instance (with some necessary adaptation by you so it works with your instance):

@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. 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'.

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

Execution of 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

@daily 0 my_php_script php -f /srv/data/web/vhosts/
上一次變更: 2017/05/24 17:58 由 Gilles L. (Gandi)