我想从cron运行Python脚本。我使用crontab作为用户而不是root来运行命令。我的Python脚本在顶部#! /usr/bin/env python
有shebang,我做了chmod +x
它使脚本可执行。Python脚本没有在cron中运行
该脚本在从shell运行但不使用crontab时不起作用。我没有检查/var/log/cron
文件,看到脚本运行,但从stdout
或stderr
打印任何地方都没有。
最后,我做了一个小脚本,它打印日期和一个字符串,并称每分钟都有效,但这个脚本没有。我不知道为什么我收到这些变化的结果...
这是我在crontab
SHELL=/bin/bash
#min #hour day-of-month month day-of-week command
#-------------------------------------------------------------------------
*/5 * * * * /path/to/script/script.py
这里条目我的脚本的源代码不会从crontab中运行,但会从运行它在被调用的时候就像是这样的./script.py
。该脚本是可执行的我用chmod +x
命令后,就可以了,顺便说一句...:
#! /usr/bin/env python
#create a file and write to it
import time
def create_and_write():
with open("py-write-out.out", "a+") as w:
w.write("writing to file. hello from python. :D\n")
w.write("the time is: " + str(time.asctime()) + "\n")
w.write("--------------------------------\n")
def main():
create_and_write()
if __name__ == "__main__":
main()
编辑 我想通了什么事情错了。我需要将绝对文件路径放在脚本中,否则它会写入我没有计划的某个目录。
请包括相关的代码 - 这将帮助我们回答。 – l0b0 2014-09-25 23:01:48
在可能影响脚本的shell环境之间经常会有其他差异:不同的'PYTHONPATH'是一种可能性。您可能希望在'cron'下运行一个脚本,该脚本只是打印其环境,并将其与基于shell的手动执行的期望进行比较。 – 2014-09-25 23:42:56
你重定向stdout和stderr吗?你期望看到它们出现在哪里? – cdarke 2014-09-26 04:59:47