How to run python scripts via cron on Linux

Description of the process of running python scripts on a Linux server.

Python alternatives to cron are timer or periodic tasks (celery).

Server preparation

The server must be configured to work with python. I will repeat the main points from the article at the link.

The python packages must be installed on the server:

sudo apt install python3-pip python3-dev python3-setuptools build-essential libssl-dev libffi-dev

The python3-venv package is installed:

sudo apt install python3-venv

Put the files in the required directory and create a virtual environment there:

File preparation

At the top of the python file, add a line leading to the python virtual environment:

#! / home / user-name / project / venv / bin / python

Make file executable:

chmod + x

If you need dependencies (modules) for the file, you can install them separately through the requirements.txt file.

Adding to cron

There is a separate article on working with cron in Linux.

Open crown file:

Add our script:

0 12 * * * ~/project/app/ > ~/project/logs/python_script.log 2> &one

The line consists of: startup time / startup script / startup log (if there are no errors, it will be empty if nothing is specifically output there).

Possible mistakes

The path to the modules is not visible. Use this article.

Similar Posts

Leave a Reply Cancel reply