【发布时间】:2020-05-08 08:34:27
【问题描述】:
目前我有一个具有以下结构的 python 脚本
read_data()
print("debugging statement 1")
# random code
plot_data(data)
print("debugging statement 2")
#random code
if __name__ == '__main__':
var = "test1"
var2 = "test2"
data_organized = read_data(var,var2)
print("debugging statement 3")
plot_data(data_organized)
.....rest of code .....
我 ssh 到一个树莓派,然后从终端我可以写:
/full/path/to/env/python3 /full/path/to/script.py
并且脚本执行完全没有错误。
我把它放在一个 crontab 中,用下面的 crontab 每分钟运行一次
*/1 * * * * /full/path/to/env/bin/python3 /full/path/to/script/script.py >> /random/path/to/where/i/want/output/cron_log.txt 2>&1
但是python代码抛出了一个错误。当我检查我的 cron_log.txt 文件时,我看到错误发生在 read_data() 函数的某处,因为我可以看到 print("debugging statement 3) 输出与应有的不同。但我看不到任何打印read_data() 函数内部的语句。
问题:
1.) 为什么我直接运行代码会完美运行,但使用 crontab 运行时却报错?
2.) 我的 crontab 条目是否正确?我希望它每分钟运行一次
3.) 如何在 read_data()、plot_Data() 函数中编写 print() 函数的输出?目前我只能在输出文件中看到 print(debugging3)。
【问题讨论】: