How do I schedule a Python script to run every day?
poze a month ago533 gadeen
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.
Dyagnostik IA
Se IA ki fè l. Se pa yon repons — kominote a anba a konfime oswa korije l. Toujou verifye anvan ou fye l.
1 Repons
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.
Konekte pou di lòt moun si li te mache.
Sign in and verify your email to post an answer.