【问题标题】:Crontab executes but nothing happens (python file)crontab 执行但没有任何反应(python 文件)
【发布时间】:2019-05-18 00:28:34
【问题描述】:

我设置了一个 crontab 文件来每分钟执行一个 python 文件,但是它声明它已执行但没有生成文件。

crontest1.py 文件包含:

#!/usr/local/bin/python
# -*- coding: utf-8 -*-


if __name__ == '__main__':
    f = open('/APPS/CronRun/crontest/dummy1.txt','w')
    f.write('hello world it is a file')
    f.close()

crontab 文件:

    # /etc/crontab - root's crontab for FreeBSD
#
# $FreeBSD: release/10.0.0/etc/crontab 194170 2009-06-14 06:37:19Z brian $
#
SHELL=/bin/sh
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin
#
#minute hour    mday    month   wday    who     command
#
*       *       *       *       *       root    /APPS/CronRun/crontest/crontest1.py
11      11      *       *       *       root    /usr/bin/find /APPS/* | grep python | grep core | xargs rm
14      14      17      *       *       root    /APPS/CronRun/Report/report.py

crontab 执行但没有文件出现。

我尝试了一个简单的日期>> datelog.txt 它通过 crontab 工作,但 python 文件似乎没有执行。

如果从 shell 手动执行而不是通过 crontab 执行,python 文件可以工作

我试过在crontab文件中明确说明:python /APPS/CronRun/crontest/crontest1.py,但这不起作用

我之前添加的 cron 作业可以正常运行,并且仍然每天执行,没有任何问题

例如

31,01   *       *       *       *       root    /APPS/CronRun/makelist/list.py

【问题讨论】:

  • 你重启过系统/重启过cron吗?
  • 我只需要在这里澄清一下:我使用了 cron,但仅用于可执行文件或 shell 脚本。 cron 也可以执行 Python 脚本吗?我的意思是,它不需要像python abc.py 这样的命令吗?
  • @kiner_shah 如果文件是可执行的chmod +x file.py 那么一切都很好——它只是执行程序
  • @nch 如果您将 print("hello world") 语句放在 if 块之外的某处会发生什么情况,如果您手动运行它并从任何您看到 cron 输出的地方运行它会出现这种情况
  • @nch,您可以尝试将结果记录在单独的文件中。像这样:/APPS/CronRun/makelist/list.py > /APPS/CronRun/makelist/log_python_script

标签: python cron


【解决方案1】:

试试不带 : 的代码

if __name__ == '__main__':

像这样:

#!/usr/local/bin/python
# -*- coding: utf-8 -*-

f = open('/APPS/CronRun/crontest/dummy1.txt','w')
f.write('hello world it is a file')
f.close()

也许“crontest1.py”由 crontab 作为模块执行 - 而不是“ma​​in”。

如果这样做没有帮助,请尝试更新 Python。

Python 3.1.x 及更低版本

可能是原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-24
    • 2021-10-16
    • 1970-01-01
    • 2016-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多