python - Hello World Cron 作业不工作

标签 python unix cron crontab

我只用了一个小时就学会了 cron工作有效,这就是我到目前为止所做的。我正在使用 crontab -e 添加我的 cron 命令,它是:

0/1 * * * */usr/bin/python/home/my_username/hello.py >/home/my_username/log.txt

crontab -l 确认我的命令在那里。

你好.py:

#!/usr/bin/python
# Hello world python program
print "Hello World!"

但我在日志文件中没有看到任何内容。有人可以解释我做错了什么吗?

最佳答案

实验表明 0/1似乎是问题所在。

0/1 应该等同于* .如果替换 0/1* ,它应该可以工作。

这是我的实验性 crontab:

0/1 * * * * echo 0/1  >> cron0.log
*   * * * * echo star >> cron1.log

这会创建 cron1.log但不是 cron0.log .

我会调查并找出原因 0/1不工作,但现在只需使用 *它应该有效。

更新:

foo/bar语法特定于 Vixie cron 实现,它被大多数 Linux 系统和 MacOS X 使用但并不通用。

每分钟运行一个命令的通常方法是只指定 *在第一个领域。要每 5 分钟运行一次命令, 如果您的 cron 支持它,请指定 */5 .

这是 crontab(5) 的内容手册页说:

Step values can be used in conjunction with ranges. Following a range with /<number> specifies skips of the number's value through the range. For example, 0-23/2 can be used in the hours field to specify command execution every other hour (the alternative in the V7 standard is 0,2,4,6,8,10,12,14,16,18,20,22). Steps are also permitted after an asterisk, so if you want to say "every two hours", just use */2.

我什至不确定是什么0/1意味着。

更新 2:

好的,这是我找到的。

鉴于字段 2 到 5 都是 * ,将第一个字段(指定分钟)设置为 *使作业每分钟运行一次。 */2每 2 分钟运行一次,*/3每 3 分钟运行一次。这一切都符合预期。

将第一个字段设置为 0/1 中的任何一个, 0/2 , 或 0/3导致作业仅在整点运行,即,它只相当于 0 .

这不是我从手册页中的描述所期望的。 Wikipedia quotejgritty's answer :

Some versions of cron may not accept a value preceding "/" if it is not a range, such as "0". An alternative would be replacing the zero with an asterisk.

似乎不完全正确,至少对于我使用的 Vixie cron 版本而言; 0/1毫无怨言地接受了,但这并不意味着我所期望的,它似乎也不是特别有用。

关于python - Hello World Cron 作业不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14307518/

相关文章:

python - Python 中的简单热键脚本 - 如何设置全局热键以发送文本字符串?

python - 在 SQLAlchemy 列属性中使用 python 日期函数

if 语句中无法识别 Python 变量

unix - 替换 sed 不更改文件

linux - Ubuntu 10.04 LTS Cron 作业不工作

python - 为什么使用 sudo 运行脚本或使用 crontab 计划会导致错误?

django - celery 击败: crontab executed same task TWICE (interval 10min)

python - 在 Python 3 中动态传递函数参数

c++ - 尝试将 gdb 与 MPI 一起使用时自动加载被拒绝?

string - 如何在 Bash 或 Unix shell 中检查字符串中的第一个字符?