Cron - Aimlackies/Reporter GitHub Wiki

Cron jobs are scheduled commands that are run. They can be wrote on any operating system but only run on Linux. Testing the commands work can also be done on any operating system.

Defining the commands in Flask

In Flask all commands should be added to cli.py which is located in reporter_app. The command can then be run with flask <command name>. As these are defined from within the Flask app full access to the database and the rest of the application is given and can be used in the same way to other parts of the app.

Cron jobs

Note: These instructions are wote for making cron jobs for the server which runs Ubuntu. Steps may need modification for other operating systems

Cron jobs are defined in crontab and are wrote with the following syntax.


* * * * * command to be executed
- - - - -
| | | | |
| | | | +---- day of the week (0-6) (sunday=0)
| | | +----month (1-12)
| | +----day of the month (1-31)
| +----hour (0-23)
+----min (0-59)

Each of the stars can be replaced with a number or other notation which will define how often the task should run. If a task does not need to use a certain schedule then the star should be left unchanged.

Examples

On the 1st minute of the hour

1 * * * * command to be executed

On the 1st and 20th minute of the hour

1,20 * * * * command to be executed

every 5 minutes

*/5 * * * * command to be executed

Once a day at one past midnight

1 * 0 * * command to be executed

File setup

To access the cron file enter the command crontab -e. If this is the first time you may be asked to select an editor to use. Select the one you are comfortable with.

Cron jobs by default run using sh. As we are using bash the flag SHELL is set to /bin/bash at the top of the cron file. This will set all cron jobs to use bash but this can be switched for a given task if added to the beginning of the command.

To load a crob job file from a backup the command crontab /some/shared/location/crontab.bak can be used.

Cron job command setup

To run cron jobs for our Flask app we have a basic setup that should be used on the server. This has the following syntax.

* * * * * (source ~/.bashrc && source ~/.bash_profile && conda activate aimlacReporter && flask <command to run> && conda deactivate) > /tmp/cronjob.log 2>&1

This is formed of a number of individual commands that need to be run in order. These are:

  1. source ~/.bashrc - Gives us access to Amaconda
  2. source ~/.bash_profile - Sets environmental variables (e.g. to set the Flask app and salt)
  3. conda activate aimlacReporter - Activates the conda environment for the Flask app
  4. flask <command to run> - The command we wish to run for the cron job (switching out command to run) with a defined task
  5. conda deactivate - Deactivate the conda enviroment
  6. /tmp/cronjob.log 2>&1 - Log any output (including errors) to a specified file

All commands are separated with an && and all enclosed in brackets. The && tells bash there are two separate command to run ether side of the && but if the first command fails then following commands will not be run. The brackets are so all commands contained are logged to the file we specified at the end.

To use logs postfix will need to be installed on the machine which should be configured to allow local messages to be sent.