简体   繁体   English

如何调试由 crontab 执行的 python 脚本?

[英]How to debug a python script that is executed by crontab?

Currently I have a python script that has the following structure目前我有一个 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 ..... 

I ssh into a raspberry pi and from the terminal I can write:我将 ssh 放入树莓派,从终端我可以写:

/full/path/to/env/python3 /full/path/to/script.py 

and the script executes perfectly no errors at all.并且脚本执行完全没有错误。

I placed it in a crontab to run every minute with the following crontab我把它放在一个 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

but the python code throws out an error.但是 python 代码会抛出错误。 when I check my cron_log.txt file I see the error occurred somewhere in the read_data() function because I can see that the print("debugging statement 3) output is off from what it should be. But I cannot see any of the print statements that are inside of the read_data() function.当我检查我的 cron_log.txt 文件时,我发现错误发生在 read_data() function 中的某处,因为我可以看到 print("debugging statement 3) output 与应有的不同。但我看不到任何打印read_data() function 内部的语句。

questions:问题:

1.) why would the code run perfectly when I directly run it but when I run it with crontab it throws out an error? 1.)为什么当我直接运行它时代码会完美运行,但是当我使用 crontab 运行它时会抛出一个错误?

2.) is my crontab entry correct? 2.) 我的 crontab 条目是否正确? I want it to run every minute我希望它每分钟运行一次

3.) how can I write the ouput of the print() functions within the read_data(), plot_Data() functions? 3.) 如何在 read_data()、plot_Data() 函数中编写 print() 函数的输出? currently I can only see print(debugging3) in my output file.目前我只能在我的 output 文件中看到 print(debugging3)。

Try with something like this in another script which will call your script.py尝试在另一个脚本中使用类似的东西,它会调用你的script.py

#!/usr/bin/env python
# You can name me: logger.py
import subprocess
with open("output.txt", "w+") as output:
    subprocess.call(["python", "./script.py"], stdout=output);

The most likely reason for the error is a permissions issue.错误的最可能原因是权限问题。 (see this https://unix.stackexchange.com/questions/81805/what-are-the-runtime-permissions-of-a-cron-job ) (请参阅此https://unix.stackexchange.com/questions/81805/what-are-the-runtime-permissions-of-a-cron-job

To debug the process, you could use a tool like remote_pdb and then step through your script.要调试该过程,您可以使用remote_pdb 之类的工具,然后单步执行您的脚本。

Answering your questions:回答您的问题:

  1. because cron jobs run in a very different environment compared to your terminal.因为与您的终端相比,cron 作业在非常不同的环境中运行。 most likely cron jobs are executed with the different permissions and from a different folder, also you cannot rely on PATH variable being the same (or any other variable for that matter)最有可能的 cron 作业是使用不同的权限并从不同的文件夹执行的,您也不能依赖 PATH 变量相同(或任何其他变量)

  2. that seems ok.这似乎没问题。

  3. your script is breaking in read_data() , once you fix this you'll see your debugging output你的脚本在read_data()中中断,一旦你解决了这个问题,你会看到你的调试 output

Finally, why it does not work and how to fix it.最后,为什么它不起作用以及如何解决它。 Since you haven't provided any meaningful sources, I'd say your read_data() function tries to access some data files using relative paths and fails, since the script is executed from a different folder.由于您没有提供任何有意义的来源,我会说您的read_data() function 尝试使用相对路径访问一些数据文件并失败,因为脚本是从不同的文件夹执行的。 You should make sure all paths are absolute and the permissions are set to allow access to those files.您应该确保所有路径都是绝对路径,并且权限设置为允许访问这些文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM