我不知道PYTHON但是我必须在Linux(CentOS6)服务器上运行一个脚本作为cronjob而且我遇到了一些问题。
如果我从浏览器(http://xx.xx/xx.py)或bash运行脚本,该脚本运行良好。在这两种情况下,它都会加载模块并更新MySql DB。
问题在于它是由 CRON :
运行的/usr/local/bin/python2.7 /home/itsme/cron/demo.py>> /home/itsme/cron/logs/cron_log.log
错误日志我有:
Failed to connect to MySql DB using connect string- DRIVER={MySQL};SERVER=localhost;DATABASE=dbname;UID=admin;PWD=*******# .Reconnect Attempt# 1
<class 'pyodbc.Error'> ('IM002', '[IM002] [unixODBC][Driver Manager]Data source name not found, and no default driver specified (0) (SQLDriverConnect)')
这意味着找不到MySql驱动程序。所以我尝试: #odbcinst -j
unixODBC 2.2.14
DRIVERS............: /etc/odbcinst.ini
SYSTEM DATA SOURCES: /etc/odbc.ini
FILE DATA SOURCES..: /etc/ODBCDataSources
USER DATA SOURCES..: /root/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8
VI /etc/odbcinst.ini
# Example driver definitions
# Driver from the postgresql-odbc package
# Setup from the unixODBC package
[PostgreSQL]
Description = ODBC for PostgreSQL
Driver = /usr/lib/psqlodbc.so
Setup = /usr/lib/libodbcpsqlS.so
Driver64 = /usr/lib64/psqlodbc.so
Setup64 = /usr/lib64/libodbcpsqlS.so
FileUsage = 1
# Driver from the mysql-connector-odbc package
# Setup from the unixODBC package
[MySQL]
Description = ODBC for MySQL
Driver = /usr/lib/libmyodbc5.so
Setup = /usr/lib/libodbcmyS.so
Driver64 = /usr/lib64/libmyodbc5.so
Setup64 = /usr/lib64/libodbcmyS.so
FileUsage = 1
如果我从我拥有的脚本中打印 sys.path :
['/home/itsme/cron/', '/usr/local/lib/python2.7/site-packages/setuptools-7.0-py2.7.egg',
'/usr/local/lib/python2.7/site-packages/pip-1.5.6-py2.7.egg',
'/usr/local/lib/python27.zip', '/usr/local/lib/python2.7',
'/usr/local/lib/python2.7/plat-linux2', '/usr/local/lib/python2.7/lib-tk',
'/usr/local/lib/python2.7/lib-old', '/usr/local/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/site-packages', '/home/itsme/cron/src', '/usr/lib']
为什么CRON无法找到MySQL驱动程序?
感谢您的帮助。
答案 0 :(得分:2)
我认为您在cron环境中遗漏了一些导致此问题的环境变量。
在终端中,从此脚本的工作位置开始,运行以下命令
printenv > env.normal.text
现在,运行一个虚拟cron作业,输出如下所示的环境变量
* * * * * * printenv > env.cron.text
现在只需比较两个相关的缺失变量。把它放在你的cron中。