Scheduling jobs with cron

In servers running on the Faculty platform, you can configure scripts to run on a regular schedule using the standard Cron utility. For example, if you have a Python script which scrapes data from a website and saves it to a CSV file in your workspace, you can use cron to run the script automatically every morning at 7 a.m., so the new data is ready when you get to your desk.

Note

In general, we recommend that you schedule recurring behaviour with jobs rather than keeping a server alive just for running cron. Jobs will create compute resources dynamically as needed, provide persistent access to logs and are generally easier to use than Cron on a server.

Cron is configured using a plain text file called a crontab. To edit your crontab in your default editor, run:

crontab -e

Each job is defined on a single line in the crontab file. The line must start with five numbers separated by spaces, which denote the minute, hour, day, month of the year, and day of the week the job should be run on. An asterisk for any field indicates the job should be run on every value of that field. crontab.guru is a useful tool that makes it easier to design your crontab rule.

The command to be run should follow the time fields. For example, the following line in your crontab will run /usr/bin/true at 4.20 p.m. every Monday:

20 16 * * 1 /opt/anaconda/envs/Python3/bin/python /project/script.py

Note

Cron jobs run from your home directory, /home/faculty, not the directory where you ran crontab -e to add the job. As in the above example, you should therefore use absolute paths both in the crontab entry and in any paths inside your script. If you are unsure of the absolute path for an executable like python, run which <executable> in the terminal.

To list your existing cron jobs, use

crontab -l

Using cron on Faculty servers

By default, Cron provides a more restricted environment than is available to a script run directly from a shell. Scripts run in Cron are not able to find programs installed into custom paths, such as Conda environments, and don’t have access to your Faculty credentials, for example to create new reports or use faculty.

Therefore, Faculty servers contain a tool called faculty-cron which will run a program from cron with the same environment as if you ran the script from a shell. Programs run via faculty-cron will have the default Conda environment activated, and access to your Faculty credentials. Simply prefix the normal command in your crontab file with faculty-cron. For example, to run a Python script in your project workspace at 7 a.m. every morning, use:

0 7 * * * faculty-cron /opt/anaconda/envs/Python3/bin/python /project/example.py

Maintenance window

The maintenance window for Faculty is from 0200 to 0600 UTC each day. Cron jobs scheduled to run on interactive servers during these hours may occasionally not run, if server maintenance is taking place.