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.
To configure anacron, you edit the anacrontab file. There is only one anacrontab per Simple Hosting instance. It is located here:
Via SFTP
/lamp0/etc/cron/anacrontab
Via SSH Console
/srv/data/etc/cron/anacrontab
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.
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!
You specify the frequency with two parameters, a frequency index and multiplier.
Frequency indices are (from smallest to largest):
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 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.
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.
The usage is similar to MySQL, the pg_dumpall command exports all postgresql databases on the instance.
@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
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
Anacron has the added benefit that if your instance is ever offline (full disk), that when your instance is started again any tasks that were scheduled to run while the instance was offline will run. With cron those tasks would be skipped, and you could miss out on potentially important backups tasks.
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 instance was started, plus a random delay to spread the load from multiple instances on a single node.