Scheduled jobs can be convenient to automate the maintenance or administration of a website application. You can create cron jobs per Container on your Cloud Container server.
You need a SSH/SFTP user linked to at least one Container to manage the scheduled jobs. If you use a SSH/SFTP user linked to a single Container you can follow these steps to add or edit your cron jobs:
/container/crontabs
directory.crontab
file accordingly.You can use the command below to edit the file through an SSH session.
nano /container/crontabs/crontab
This crontab file uses the standard cron format and you can find more information about this on the Cron Wikipedia page. The Contrab Guru helper is handy if you would like to check your crontab set up.
Cloud Containers run on New Zealand time, i.e. NZST (UTC+12) or NZDT (UTC+13), consider this when configuring your scheduled jobs.
You can use environment variables in your scheduled jobs.
To do so, you will need to modify the cron command to export the environment variables and push it to a place where the cron command can use it. You can use the command below to edit the necessary config file.
nano /container/config/supervisord.conf
You will want to find the section that looks like this:
[program:cron]
command=/bin/bash -c "touch /etc/crontab && cron -f"
You will need to change it to look like the below:
[program:cron]
command=/bin/bash -c "touch /etc/crontab && printenv >> /etc/environment && cron -f"
You will need to restart the container to apply these changes.
This will push all the environment variables into the /etc/environment
file, which will allow the cronjobs to use the environment variables.
Be aware that this will provide all of your environment variables to the cronjobs. This may include sensitive information.
There are other ways of passing environment variables to cronjobs, but this is the simplest way to do it. If you require special characters, or only want a certain cronjobs to use these environment variables, you can use the below line:
* * * * * export $(cat [/path/to/environment/variables] | xargs) && [your cronjob here]
Alternatively, if you wish for cron specific environment variables, you can add them to the crontab file directly. This can be found under /container/crontabs/crontab
.
MYVAR=VALUE
The jobs will be executed as the user www-data (uid: 33) on the Cloud Container servers. The www-data user should have access to all files and directories within the /container
directory but the /container/backup/containers
directory.
If your scheduled jobs are not working properly, the tips below can help you track down the issue:
MAILTO
variable, so you can receive the outputs of the command set when the task is processed. If no output is thrown, you won't receive emails./container/logs/rsyslog/syslog
log file. The lines as per the below indicate your crontab file is being processed.
CRON[XX]: (root) CMD (/bin/cat /cron/crontab | /usr/bin/crontab -u www-data -)
nano
or vim
text editors.In order to make the Cloud Containers as generic as possible, we had to make some choices and the mapping of the directory differs from what you can see in your SSH session. If you need to execute files under the application directory, or write some information into a log file, consider using /container/
instead of the paths you can see when connected via SSH.
Here is the matrix of the mapping from your ssh connection to the container:
Path to use | Equivalent path |
---|---|
/container/application | /data/docker_app |
/container/logs | /var/log/docker_app |
/container/config | /etc/docker_app |