尽管手动运行时Python脚本未在cron下运行 [英] Python script is not running under cron, despite working when run manually

查看:85
本文介绍了尽管手动运行时Python脚本未在cron下运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道对此有很多疑问,但是我已经尝试了好一阵子了,但是没有运气.我有一个简单的python testscript,它创建一个带有时间戳的文件夹.当我手动运行它时,它可以完美工作,但是当我尝试将其放入crontab时,什么也没有发生.知道我在这里搞砸了吗?这是脚本:(位于/home/ec2-user/cronscripts中)

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. It works perfectly when I run it manually, but nothing happens when I try to put it into crontab. Any idea where I'm messing up here? Here's the script: (located in /home/ec2-user/cronscripts)

from subprocess import call
from time import time
import math

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

和crontab -l产生输出

and crontab -l produces the output

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

我也尝试将'root'放入命令中,但是没有骰子.我还尝试过一些在脚本文件中显式设置PATH变量的问题.

I've tried putting 'root' into the command as well, but no dice. I've also tried some to mess around with explicitly setting the PATH variable in the script file.

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

这是我在根邮件中找到的最后一个条目.它似乎无法找到python,即使它正好位于该位置.当我打印crontab -l时,usr/bin/python确实有一个斜杠.我需要在某处更改cron路径吗?

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. And when I print crontab -l, usr/bin/python does have a leading slash. Do I need to change a cron path somewhere?

推荐答案

为crontab中的所有命令使用完全限定的路径.

Use fully qualified path for all commands in 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.

这篇关于尽管手动运行时Python脚本未在cron下运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆