Crontab没有执行Python脚本? [英] Crontab not executing a Python script?

查看:127
本文介绍了Crontab没有执行Python脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的python脚本不在crontab下运行。

My python script is not running under my crontab.

我已将其放在python脚本的顶部:

I have placed this in the python script at the top:

#!/usr/bin/python

我尝试这样做:

chmod a+x myscript.py

已添加到我的 crontab -e

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=""

* * * * * /home/me/project/myscript.py

我的/ var / log / cron文件说:

My /var/log/cron file says:

Sep 21 11:53:02 163-dhcp /USR/SBIN/CROND[2489]: (me) CMD (/home/me/project/myscript.py)

但是我的脚本未运行,因为当我检查sql数据库时,什么都没有改变。如果我像这样直接在终端中运行它:

But my script is not running because when I check my sql database, nothing has changed. If I run it directly in the terminal like so:

python /home/me/project/myscript.py

我得到正确的结果。

这是 myscript.py

#!/usr/bin/python

import sqlite3

def main():
    con = sqlite3.connect("test.db")

    with con:

        cur = con.cursor()

        cur.execute("CREATE TABLE IF NOT EXISTS testtable(Id INTEGER PRIMARY KEY, Name TEXT)")

        cur.execute("INSERT INTO testtable(Name) VALUES ('BoB')")

        cur.execute("SELECT * FROM testtable")

        print cur.fetchall()    

if __name__ == "__main__":
    main()

每个注释:是的, / usr / bin / python 存在。我还可以直接使用 /home/me/project/myscript.py 直接运行python脚本。 / usr / bin / python /home/me/project/myscript.py 可以工作。所以我不相信这是原因吗?

Per comments: Yes, /usr/bin/python exists. I can also run the python script directly using just /home/me/project/myscript.py. /usr/bin/python /home/me/project/myscript.py works. So I don't believe this is the cause?

推荐答案

键入

/home/me/project/myscript.py 进入外壳?

可以在您的crontbb命令中明确使用 / usr / bin / python

Can you explicitly use /usr/bin/python in your crontbb command?

您可以使用绝对路径吗? test.db cd 到正确的目录,然后执行python脚本?

Can you either use an absolute path to your test.db or cd to the correct directory then execute your python script?

这对在Python中包含调试语句并记录一些数据很有帮助。 Crontab调试起来可能非常棘手。

This is helpful to have debug statements in your python and log some data. Crontab can be very tricky to debug.

这篇关于Crontab没有执行Python脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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