Running a Python script on a schedule in cPanel

To run a Python script every day or every hour in cPanel, the recipe is: create a Python application (just to get the virtual environment with your libraries) and make the cron job call that environment’s Python, with the script’s full path. The commonest mistake is calling the system’s Python, which does not have the libraries you installed.

Step by step

1 Create the application in “Setup Python App”. Choose the Python version and a folder for the application. It does not have to be a website: the application serves as the environment. See Python in cPanel: virtual environments and pip.
2 Install the libraries inside that environment, with the application’s own pip and a requirements.txt file. Outside the environment, the libraries do not end up where the script will look for them.
3 Put the script in a folder outside public_html, and use full paths in it for every file it reads or writes. Cron does not start in your folder.
4 Find the environment’s Python. The application screen shows the command that activates it, with a path ending in bin/activate. Copy that path and swap activate for python: that is the environment’s Python.
5 Write the cron line: the environment’s Python path, a space, the script path, and >> /home/ACCOUNT/job.log 2>&1 at the end. For the schedule, see cron schedule examples.
6 Test the same line by hand in the Terminal, if your account has one, before you schedule it.

What usually goes wrong

Symptom Cause What to do
ModuleNotFoundError only in cron Cron used the system’s Python. Use the virtual environment’s Python path.
It cannot find a config file A relative path. Full paths, or work out the folder from the script itself.
The log stays empty or only fills at the end Output is held in memory and only comes out when the program ends. Run with python -u or write flush=True in your print calls.
Two runs at the same time The first has not finished when the second starts. Lengthen the interval, or use a lock file.
The application’s variables do not exist in cron Variables set on the application screen belong to the application, not to cron. Read the configuration from a file outside public_html.
Long, heavy scripts do not belong on a shared plan. There are limits on CPU, memory and processes, and a script that exceeds them is stopped. Split the work into short parts, or use a VPS. See the limits nobody advertises.
Write an end line to the log, with the date and the result. A log that finishes is proof the script reached the end and was not cut off halfway.

Want a plan with Python and cron in cPanel? See the hosting plans.

See the plans

SEE ALSO

Python in cPanel: virtual environments and pip

Cron jobs: what they are for and how to create one

A cron job that does not run or sends no output: a checklist

RECOMMENDED PRODUCT

Web hosting with cPanel

Domain and SSL included, daily backups and the panel you already know. from $6.59/mo (3-year plan, with coupon)

See plans
  • 0 Users Found This Useful
Was this answer helpful?