简体   繁体   English

Python 脚本没有在 cron 下运行,尽管在手动运行时可以工作

[英]Python script is not running under cron, despite working when run manually

I know there have been plenty of questions about this, but I've been trying things for a while with no luck.我知道有很多关于这个的问题,但我已经尝试了一段时间但没有运气。 I have a simple python testscript that creates a folder with a timestamped name.我有一个简单的 python 测试脚本,它创建一个带有时间戳名称的文件夹。 It works perfectly when I run it manually, but nothing happens when I try to put it into crontab.当我手动运行它时它工作得很好,但是当我尝试将它放入 crontab 时没有任何反应。 Any idea where I'm messing up here?知道我在这里搞砸了吗? Here's the script: (located in /home/ec2-user/cronscripts)这是脚本:(位于/home/ec2-user/cronscripts)

from subprocess import call
from time import time
import math

call(["mkdir","derp" +str(math.floor(time()))])

and crontab -l produces the output和 crontab -l 产生输出

* * * * * python /home/ec2-user/cronscripts/testscript.py

I've tried putting 'root' into the command as well, but no dice.我也试过将“root”放入命令中,但没有骰子。 I've also tried some to mess around with explicitly setting the PATH variable in the script file.我还尝试了一些在脚本文件中显式设置 PATH 变量的方法。

From root@domU-12-31-38-00-AC-02.compute-1.internal  Wed Jun 15 19:57:01 2011
Return-Path: <root@domU-12-31-38-00-AC-02.compute-1.internal>
Received: from domU-12-31-38-00-AC-02.compute-1.internal (localhost [127.0.0.1])
        by domU-12-31-38-00-AC-02.compute-1.internal (8.14.4/8.14.4) with ESMTP id p5FJv1aS006094
        for <root@domU-12-31-38-00-AC-02.compute-1.internal>; Wed, 15 Jun 2011 19:57:01 GMT
Received: (from root@localhost)
        by domU-12-31-38-00-AC-02.compute-1.internal (8.14.4/8.14.4/Submit) id p5FJv1Dc006093;
        Wed, 15 Jun 2011 19:57:01 GMT
Date: Wed, 15 Jun 2011 19:57:01 GMT
Message-Id: <201106151957.p5FJv1Dc006093@domU-12-31-38-00-AC-02.compute-1.internal>
From: root@domU-12-31-38-00-AC-02.compute-1.internal (Cron Daemon)
To: root@domU-12-31-38-00-AC-02.compute-1.internal
Subject: Cron <root@domU-12-31-38-00-AC-02> usr/bin/python /home/ec2-user/cronscripts/testscript.py
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>

/bin/sh: usr/bin/python: No such file or directory

This was the last entry I found in the root mail.这是我在根邮件中找到的最后一个条目。 It seems to not be able to find python, even though it's exactly where that say it is.它似乎无法找到python,即使它正是所说的地方。 And when I print crontab -l, usr/bin/python does have a leading slash.当我打印 crontab -l 时,usr/bin/python 确实有一个前导斜杠。 Do I need to change a cron path somewhere?我需要在某处更改 cron 路径吗?

Use fully qualified path for all commands in crontab.对 crontab 中的所有命令使用完全限定的路径。

0 */2 * * * /full/path/to/python /home/ec2-user/cronscripts/testscript.py

Also, all fields are *?此外,所有字段都是 *? When do you expect this to be run?你预计什么时候运行? The above example would run every other hr.上面的例子将每隔一个小时运行一次。

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

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