Python脚本在cron中未正确执行
我尝试在cron中自动化python脚本时遇到了一个问题。我相信问题在于通过cron运行脚本时没有导入mysql模块 我在网上尝试了不同的解决方案,但似乎都不起作用 顺便说一句,在终端中执行时脚本运行正常 这是我的crontab:Python脚本在cron中未正确执行,python,mysql,cron,Python,Mysql,Cron,我尝试在cron中自动化python脚本时遇到了一个问题。我相信问题在于通过cron运行脚本时没有导入mysql模块 我在网上尝试了不同的解决方案,但似乎都不起作用 顺便说一句,在终端中执行时脚本运行正常 这是我的crontab: SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin:$HOME/bin PYTHONPATH=/usr/lib/python2.7/site-packages #MAILTO=root # For details se
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin:$HOME/bin
PYTHONPATH=/usr/lib/python2.7/site-packages
#MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
* * * * * /usr/bin/python /var/www/html/testlist.py > /var/log/test.log 2>&1
下面是test.log中生成的错误
Traceback (most recent call last):
File "/var/www/html/testlist.py", line 106, in <module>
if __name__ =='__main__':main()
File "/var/www/html/testlist.py", line 104, in main
get_ec2_instances('us-west-1')
File "/var/www/html/testlist.py", line 90, in get_ec2_instances
insert_metric(d[u'Timestamp'],d[u'Average'],d[u'Unit'])
File "/var/www/html/testlist.py", line 49, in insert_metric
cursor.close()
UnboundLocalError: local variable 'cursor' referenced before assignment
谢谢你看过回溯了吗???问题很清楚:在
finally
子句中,您试图访问尚未定义的名称cursor
查看您的代码:您正在try
块的第三行定义光标
——因此,在您的代码到达该点之前,名称光标
不存在。现在,如果前两条语句中的一条引发了异常,那么您将进入finally
块,在那里您将尝试访问游标
try:
db_config = read_db_config()
conn = MySQLConnection(**db_config)
cursor = conn.cursor()
cursor.execute(query, args)
if cursor.lastrowid:
print('last insert id', cursor.lastrowid)
else:
print('last insert id not found')
conn.commit()
except Error as error:
print(error)
finally:
cursor.close()
conn.close()
要解决这个(第一个)问题,您需要在try块之前有一个更窄的try块和/或定义光标
(和conn
)
首先,从try块中提取对read\u db\u config
的调用-这里不需要它。如果失败了,你无论如何都会有一个回溯。。。然后在try块之前将conn
和cursor
定义为None
,这样在finally块中就不会出现namererror
,在finally块中,在关闭它们之前测试cursor
和conn
是否已打开。此外,您的except
子句非常无用-它阻止您获得完整的回溯(这对于调试非常重要),因此只需删除它并让异常传播:
conn = None
cursor = None
db_config = read_db_config()
try:
conn = MySQLConnection(**db_config)
cursor = conn.cursor()
cursor.execute(query, args)
if cursor.lastrowid:
print('last insert id', cursor.lastrowid)
else:
print('last insert id not found')
conn.commit()
finally:
if cursor is not None:
cursor.close()
if conn is not None:
conn.close()
现在,您可以再次运行代码,这一次可以找出真正的问题所在(这显然与导入MySQLdb无关-否则您将从一开始就得到一个
ImportError
,您的代码甚至不会被调用)。在try
-套件中发生异常。因此,游标
未成功定义。错误消息应该在/var/log/test.log
中打印出来。上面写的是什么?错误就贴在上面了。这是关于UnboundLocalError的:在赋值之前引用了局部变量“cursor”。我在模拟时遇到了相同的错误,删除了import mysql.connector,然后在shell中执行脚本。如果删除try
,除了,最后
行,删除里面的代码,只运行“裸”代码,会发生什么?然后您应该在test.log
中看到真实原始异常的回溯错误消息。谢谢!当我删除try
,时,我得到了真正的罪犯,除了,最后
。问题出在mysql_connector.py中。我无法使用其绝对路径定义配置文件。谢谢@unutbuThank你,我知道问题的真正原因了。问题出在mysql_connector.py中。我无法在脚本中使用其绝对路径定义配置文件。非常感谢。
conn = None
cursor = None
db_config = read_db_config()
try:
conn = MySQLConnection(**db_config)
cursor = conn.cursor()
cursor.execute(query, args)
if cursor.lastrowid:
print('last insert id', cursor.lastrowid)
else:
print('last insert id not found')
conn.commit()
finally:
if cursor is not None:
cursor.close()
if conn is not None:
conn.close()