简体   繁体   English

在debian上通过crontab执行python脚本要疯了!

[英]Going nuts with executing python script via crontab on debian!

This is what my crontab file looks like: 这是我的crontab文件的样子:

* * * * *  root /usr/bin/python /root/test.py >> /root/classwatch.log 2>&1

This is what my python script looks like: 这是我的python脚本的样子:

#!/usr/bin/python
print "hello"

The cronjob creates the log file. cronjob创建日志文件。 But it is empty. 但它是空的。 I am also pretty certain that the python file is not being executed. 我也很确定python文件没有被执行。

Appreciate any help! 感谢任何帮助! I've been playing with it for past 4 hrs with no luck. 我已经玩了四个多小时,没有运气。

There are two ways to create a crontab -- per user or globally. 有两种创建crontab的方法-每个用户或全局。 For the global crontab (/etc/crontab) you specify the user, as per: 对于全局crontab(/ etc / crontab),您可以按照以下方式指定用户:

# m h dom mon dow user  command
17  *   *   *   *   root        cd / && run-parts --report /etc/cron.hourly

For user crontabs you don't, as per: 对于用户crontab,您不需要这样做,具体如下:

aj@wherever:~$ crontab -l
0 * * * * /home/aj/bin/update-foobar

To get a python script running via #! 要通过#运行python脚本! notation, you just make the script executable (chmod 755 /root/test.py), and invoke it directly, something like: 表示法,您只需使脚本可执行(chmod 755 /root/test.py),然后直接调用它即可,例如:

/root/test.py

If you don't want to do that, you can run it via the python interpretor by hand, like: 如果您不想这样做,则可以通过python解释器手动运行它,例如:

/usr/bin/python /root/test.py

This assumes whichever user you're running as (ie the user in /etc/crontab, or the user you're running crontab -e as) has permission to see the python script -- /root might be inaccessible to regular users, eg. 这假定您以哪个用户身份运行(即/ etc / crontab中的用户,或以crontab -e作为身份运行的用户)都有权查看python脚本-普通用户可能无法访问/ root,例如。

You can get a good idea of whether your script is being executed at all by adding: 通过添加以下内容,您可以很好地了解脚本是否正在执行:

import time
time.sleep(20)   # pause for 20 seconds

and then checking with "top" or "ps aux" or "pstree" to see if python's actually running. 然后使用“ top”或“ ps aux”或“ pstree”进行检查,以查看python是否真正在运行。

Updated... 更新...

Replace the contents with 将内容替换为

* * * * * date >> /tmp/foo 

Does this link help? 这个连结有帮助吗?

Delete the file it is supposed to create. 删除它应该创建的文件。 Does it come back? 它回来了吗? I thought each user had his own crontab file so the user on the line is suspsect. 我以为每个用户都有自己的crontab文件,所以在线上的用户是可疑的。
DId someone play a joke on you and replace the python binary with a no op? 有人会在你身上开个玩笑,然后用no op替换python二进制文件吗?

I have to think cron isn't working right since the echo doesn't work. 我必须认为cron不能正常工作,因为回声不起作用。 Did you make sure to change the output directory to /tmp with the echo? 您确定通过echo将输出目录更改为/ tmp吗?

can you do an od (octal dump) of the file and see if maybe you put a control character or a tab into the cron file? 您可以对文件进行od(八进制转储),看看是否在cron文件中放入了控制字符或制表符?

chmod 755 /root/test.py

and then 然后

* * * * * /root/test.py >> /root/classwatch.log 2>&1

should work. 应该管用。

您是否尝试过将脚本放置在其他位置(例如/ usr / local / bin /)?

This works fine for me on my RHES 4 Linux box exactly as shown (NOTE: I removed the 'root' username in the crontab). 完全按照所示在RHES 4 Linux上对我来说工作正常(注意:我在crontab中删除了“ root”用户名)。

I suspect there's something wrong with the way you're installing your cron job, or the configuration of cron on your system. 我怀疑您安装cron作业的方式或系统上cron的配置有问题。 How are you installing this? 您如何安装此? Are you using crontab -e or some other method? 您在使用crontab -e还是其他方法? Are you able to run any other cron jobs for root successfully? 您是否能够为root成功运行任何其他cron作业?

It might be because of a job declared earlier failing due to syntax error. 这可能是由于先前声明的作业由于语法错误而失败。 Can you paste your entire crontab? 您可以粘贴整个crontab吗? Your line looks good as far as I can see. 据我所知,您的电话线看起来不错。

如果您正在编辑/ etc / crontab,则crontab条目是正确的-但是,如果您使用的是普通用户的crontab(即crontab -e,crontab crontabfle等),则根条目在语法上是不正确的。

Try just sending stdout to the log file, instead of both stderr and stout: 尝试仅将stdout发送到日志文件,而不要同时发送stderr和stout:
/usr/bin/python /root/test.py > /root/classwatch.log / usr / bin / python /root/test.py> /root/classwatch.log

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

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