This hands-on guide builds and verifies How to Automate Python Scripts with cron and systemd Timers on Ubuntu 24.04 using an isolated, reproducible Python environment.
1. Create an isolated project
mkdir -p python-lab
cd python-lab
python3 -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip
Record the interpreter and dependencies before changing the application. Use python -m pip so packages are installed into the selected virtual environment.
2. Implement and run
sudo systemctl daemon-reload
sudo systemctl enable --now report-job.timer
Keep transport, configuration, business rules, and persistence in separate modules. Validate all environment and network input at the application boundary, set finite timeouts, and return actionable errors without exposing stack traces or credentials.
3. Verify the result
systemctl list-timers report-job.timer
journalctl -u report-job.service -n 50 --no-pager
A running process is not enough: verify the real request, file, database operation, worker state, or deployment rollout. Capture logs and expected output for repeatable operations.
4. Troubleshooting
Confirm the active interpreter with python -c "import sys; print(sys.executable)". Then inspect dependency versions, environment variables, permissions, listening addresses, service logs, and upstream connectivity. Change one variable at a time and retest.
Production checklist
- Supported Python and dependency versions are recorded.
- Secrets remain outside code, images, logs, and shell history.
- Validation, authorization, limits, timeouts, retries, and graceful shutdown are explicit.
- Tests and health checks cover success and failure paths.
- Backups, monitoring, security updates, rollback, and resource limits are tested.
Reference: official documentation.