【问题标题】:Cron job not running pythonCron 作业没有运行 python
【发布时间】:2020-02-16 21:27:44
【问题描述】:

我试图让 cron 运行一个 python 文件,但它似乎没有运行。 cron中的命令是:

24 21 * * * python /mnt/c/Users/saifl/git/StaffTrainingLog/automate.py

日志中出现以下内容:

Feb 16 21:24:01 MSI CRON[3839]: (slatifi) CMD (python /mnt/c/Users/saifl/git/StaffTrainingLog/automate.py)

但是,它实际上并不运行代码。当我在上面的备份中运行命令时,它可以正常工作。请指教。

更新(罗密欧回应后):

我现在已将命令移至脚本 (automate_script):

#!/bin/bash
source ~/.bashrc
cd /mnt/c/Users/saifl/git/StaffTrainingLog
/usr/bin/python3 automate.py

我现在在 Crontab 中有以下内容:

50 14 * * * /mnt/c/Users/saifl/git/StaffTrainingLog/automate_script

当在命令行中运行 /mnt/c/Users/saifl/git/StaffTrainingLog/automate_script 时,它可以工作,但是,它确实使用 cron 运行,因为它出现在日志中,但没有任何反应。

【问题讨论】:

    标签: cron windows-subsystem-for-linux


    【解决方案1】:

    有趣...您的 cron 设置正确并且正在运行。问题可能出在您的脚本上。 也许您忘记为其添加 exec 权限? 尝试运行chmod +x path/to/your/script,然后再次运行您的 cron。

    【讨论】:

    • 这不是问题,因为我可以在控制台中运行 python /mnt/c/Users/saifl/git/StaffTrainingLog/automate.py 并且它会工作
    【解决方案2】:

    当你执行这样的脚本时,几乎没有什么事情要做。并且最好为此目的创建专用脚本。

    #!/bin/bash
    source ~/.bashrc
    cd /mnt/c/Users/saifl/git/StaffTrainingLog
    /path/to/python  automate.py
    
    1. 来源 .bashrc 以获取您的环境设置
    2. cd 到脚本所在目录
    3. 使用python 的绝对路径运行它

    并在 cron 中运行上述脚本

    24 21 * * * /path/to/the/script
    

    别忘了让脚本可执行

    【讨论】:

    • 谢谢 - 脚本应该是什么扩展名?
    • 我尝试在没有文件扩展名的情况下执行上述操作,但日志中出现以下内容,但仍然没有任何反应:
    • 2 月 17 日 14:17:01 MSI CRON[4235]: (slatifi) CMD (/mnt/c/Users/saifl/git/StaffTrainingLog/automate_script) 2 月 17 日 14:17:01 MSI CRON [4234]: (root) CMD (cd / && run-parts --report /etc/cron.hourly)
    猜你喜欢
    • 1970-01-01
    • 2012-08-18
    • 2014-01-07
    • 2018-06-11
    • 2014-04-04
    • 2015-01-05
    • 1970-01-01
    • 2014-07-21
    • 1970-01-01
    相关资源
    最近更新 更多