Python script works when I run it, does nothing in cron. What am I missing?
Quick one. Practicing Linux for the transition (ETS in 7 months, SkillBridge after that if it gets approved). Wrote a Python script that pings a list of hosts and writes the results to a CSV. Works fine when I run it from my home directory. Put it in crontab -e to run every 5 minutes. Nothing. No CSV, no error, nothing. */5 * * * * python3 pinger.py What I checked: cron is running ( systemctl status cron says…
Accepted answer
Brother, Jen's right, it's a path thing. Two of em actually. 1. Cron runs with a bare-bones environment. Your venv isn't activated, so python3 is the system Python, which doesn't have requests. The script dies on the import. 2. You never see the error because cron has nowhere to send the output. No mail server on that VM, so it goes in the bit bucket. Fix both in one line. Absolute paths for everything, and send the output to a log: */5 * * * * cd /home/ray/pinger && /home/ray/pinger/.venv/bin/python pinger.py >> /home/ray/pinger/cron.log 2>&1 The cd matters if your script opens hosts.txt or writes results.csv with a relative path. To confirm cron actually fired at all: grep CRON /var/log/syslog Once that works, look at systemd timers. Output goes to journalctl for free and you can test the unit by hand with systemctl start. That's what we run at work. My 2 cents.