How do I schedule a Python script to run every day?

posée a month ago539 vuesen

6

I have a script that downloads exchange rates (USD/HTG) and saves them to a database. I want it to run every morning at 7am on my Ubuntu server.

Diagnostic IA

Généré par IA. Ce n'est pas une réponse — la communauté ci-dessous le confirme ou le corrige. Vérifiez toujours avant de vous y fier.

posée a month ago

1 réponse

6
✓
✓ Réponse acceptée

Use cron. Run crontab -e and add:

0 7 * * * cd /opt/rates && /opt/rates/.venv/bin/python fetch_rates.py >> /var/log/rates.log 2>&1

Important details: - Use absolute paths. Cron has a minimal PATH and doesn't activate your venv, so call the venv's Python directly. - Redirect output to a log file, otherwise errors vanish. - Cron uses the server's timezone. Check it with timedatectl. Haiti is America/Port-au-Prince.

On a systemd-based server, a systemd timer is a good alternative with better logging (journalctl -u rates), and Persistent=true runs missed jobs after a reboot.

Also make the script idempotent: re-running it the same day should update, not duplicate.

Connectez-vous pour dire si ça a marché.

answered a month ago

Sign in and verify your email to post an answer.