Cron无法执行Python脚本排查:Windows10通过Putty调度任务异常
sowfinal.py Hey there! Let’s dig into why your sowfinal.py script won’t run via cron even though sowtest.py works perfectly—you’re right to suspect library dependencies as a key culprit, but there are a few other common issues to check too.
First, Fix the Cron Syntax & Path Issues
Looking at your cron entries, I notice two quick red flags:
- Your cron schedule has double asterisks (
** * * *)—that’s invalid; it should be five single asterisks (* * * * *) for a task that runs every minute. - The
cdcommand is missing a leading slash:cd home/db2inst1/TicketAnalyticsshould becd /home/db2inst1/TicketAnalytics. Without the slash, cron will try to change to a relative directory that doesn’t exist in its default working environment. Also, add&&between thecdand Python command to ensure the script only runs if the directory change succeeds:* * * * * cd /home/db2inst1/TicketAnalytics && /usr/local/bin/python sowfinal.py /tmp/listener.log 2>&1
Why Library Dependencies Break Cron Jobs
Cron runs in a stripped-down environment with minimal environment variables—this is the #1 reason scripts fail in cron but work in your interactive PuTTY shell. Here’s how it affects your libraries:
- Python Path Differences: When you run Python in PuTTY, your user-specific
PYTHONPATH(which includes directories like~/.local/lib/pythonX.X/site-packagesfor user-installed libraries) is loaded. Cron doesn’t load this by default, so ifsowfinal.pyuses third-party libraries installed withpip install --user, cron won’t find them. - System vs. User Libraries: If you installed libraries for your user account only (not system-wide), cron’s limited environment won’t pick them up.
sowtest.pylikely only uses standard library modules, so it avoids this problem.
How to Debug & Fix the Issue
1. Check Cron’s Error Output
Modify your cron job to redirect all output to a log file—this will show you exactly why the script fails:
* * * * * cd /home/db2inst1/TicketAnalytics && /usr/local/bin/python sowfinal.py /tmp/listener.log > /tmp/cron_sowfinal_debug.log 2>&1
After a minute, open /tmp/cron_sowfinal_debug.log. If you see ModuleNotFoundError, that confirms a library path issue.
2. Match Cron’s Environment to Your Shell
You can explicitly set the PYTHONPATH in your cron job to match what’s in your interactive shell. First, run this in PuTTY to get your user’s Python path:
echo $PYTHONPATH
Then add it to your cron entry:
* * * * * PYTHONPATH="/home/db2inst1/.local/lib/python3.8/site-packages:$PYTHONPATH" cd /home/db2inst1/TicketAnalytics && /usr/local/bin/python sowfinal.py /tmp/listener.log 2>&1
(Replace the path with whatever echo $PYTHONPATH returned for your setup.)
3. Test in a Non-Interactive Shell
Cron runs tasks as a non-interactive shell, so simulate that to replicate the issue:
su - db2inst1 -c "/usr/local/bin/python /home/db2inst1/TicketAnalytics/sowfinal.py /tmp/listener.log"
If this throws an error, you’ll see the same problem cron is facing—fix it here first, and cron will work too.
4. Use a Shebang in Your Script
Add a shebang line at the very top of sowfinal.py to ensure it uses your desired Python interpreter:
#!/usr/local/bin/python
Then make the script executable:
chmod +x /home/db2inst1/TicketAnalytics/sowfinal.py
Now your cron job can simplify to:
* * * * * cd /home/db2inst1/TicketAnalytics && ./sowfinal.py /tmp/listener.log 2>&1
内容的提问来源于stack exchange,提问作者sowmith reddy

