我使用 cron 每 30 分钟执行一个 python 脚本,但它在 import 语句处停止,我不明白为什么,因为我没有得到任何反馈。
在我的 crontab 中我有*/30 * * * * sh exec_script.sh
文件exec_script.sh
is
cd /home/ziofil/python_scripts
python script_30_mins.py
在 python 脚本中我有
import logging
logging.basicConfig(filename="explicit_log.txt",level=logging.DEBUG)
logger = logging.getLogger(__name__)
logger.debug("script is executed")
# in the python_scripts folder there is a subfolder utilities/ that
# contains utility_1.py, were I define the class Utility
from utilities.utility_1 import Utility
logger.debug("Utility loaded")
utility = Utility()
logger.debug("object creation")
utility.do_your_thing()
logger.debug("done its thing")
如果我执行python script_30_minutes.py
从终端来看,一切工作正常,但是使用 cron 时,执行会停止在import
声明(我读到“脚本已执行”,但没有其他内容)。
出了什么问题?
更新:我将导入语句放在try/except
块并记录了异常:“没有名为utilities.utility_1的模块”。我还尝试将 shell 脚本中的 PYTHONPATH 变量设置为/home/ziofil/python_scripts
,但还是不行。
我发现问题出在哪里了。 @UweMannl 一直都是对的。在 @mhawke 指出 cron 的环境与我的终端环境不同之后,我想也许 python 二进制文件也可能不同,而且确实是:我想要/home/ziofil/anaconda3/bin/python
并且 cron 正在使用/usr/bin/python
.
我将脚本的最后一行修改为/home/ziofil/anaconda3/bin/python script_30_mins.py
一切正常!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)