简体   繁体   English

从Cron.d运行Python脚本

[英]Running Python Script from Cron.d

I'm trying to run a Django management command via a crontab installed to /etc/cron.d/mycron on Ubuntu. 我正在尝试通过在Ubuntu上安装到/etc/cron.d/mycron的crontab运行Django管理命令。

I first tested my basic setup by writing the following to /etc/cron.d/mycron: 我首先通过将以下内容写入/etc/cron.d/mycron测试了我的基本设置:

* * * * * root command echo "Test $(date)" 2>&1 >> /tmp/mycron.log

And I confirmed /tmp/mycron.log was updated once a minute and contains the expected text. 并且我确认/tmp/mycron.log每分钟更新一次,并且包含预期的文本。

I then tried the following variations using my actual management command: 然后,我使用实际的管理命令尝试了以下变体:

* * * * * root command python /path/to/my/script/manage.py mycommand 2>&1 >> /tmp/mycron.log

* * * * * root command /usr/bin/python /path/to/my/script/manage.py mycommand 2>&1 >> /tmp/mycron.log

* * * * * root command /path/to/my/script/manage.py mycommand 2>&1 >> /tmp/mycron.log

* * * * * python /path/to/my/script/manage.py mycommand 2>&1 >> /tmp/mycron.log

* * * * * /path/to/my/script/manage.py mycommand 2>&1 >> /tmp/mycron.log

* * * * * /usr/bin/python /path/to/my/script/manage.py mycommand 2>&1 >> /tmp/mycron.log

And even though the log file is generated, it contains none of the expected logging text my management command outputs. 即使生成了日志文件,它也不包含我的管理命令输出的预期日志文本。

After each variation, I run sudo touch /etc/cron.d/ to reload cron. 每次修改后,我运行sudo touch /etc/cron.d/重新加载cron。 Even if there was an error in my Python code, I'd expect some sort of error message to get logged. 即使我的Python代码有错误,我也希望记录一些错误消息。

I've checked tail -f /var/log/syslog , and the only errors it shows are for crontabs that don't start with "root", for which it gives me the error: 我已经检查了tail -f /var/log/syslog ,它显示的唯一错误是不是以“ root”开头的crontabs的错误:

Error: bad username; while reading /etc/cron.d/mycron

For all the others, I see something like: 对于其他所有人,我看到类似以下内容的东西:

Oct  7 10:54:01 localhost CRON[27805]: (root) CMD (/path/to/my/script/manage.py mycommand 2>&1 >> /tmp/mycron.log)

But nothing's written to the log, and the database changes I expect to happen aren't occuring. 但是什么都没有写到日志中,并且我期望发生的数据库更改也没有发生。 What am I doing wrong? 我究竟做错了什么?

The output you need is either in /tmp/mycron.log , or mailed to root . 您需要的输出在/tmp/mycron.log ,或邮寄到root Type mail from under root to check. root下键入mail进行检查。

Also, why are you trying different variations? 另外,为什么要尝试其他变化形式? The syntax of crontab files is strictly defined ( man crontab -S 5 ), for /etc/crontab it is: 严格定义了crontab文件的语法( man crontab -S 5 ),对于/ etc / crontab它是:

min hour day month weekday user cmdline

for personal crontabs, it is: 对于个人crontab,它是:

min hour day month weekday cmdline

You can't omit user name in /etc/crontab, neither you can use /usr/bin/python in place of user name. 您不能在/ etc / crontab中省略用户名,也不能使用/usr/bin/python代替用户名。

You also don't need the word command , it only works because there is built-in bash function with this name which apparently just runs a command from it's arguments. 您也不需要单词command ,它仅能工作,因为有一个内置的bash函数具有该名称,该函数显然只是从其参数运行命令。

The correct line must be either of: 正确的行必须是以下任意一项:

* * * * * root /path/to/my/script/manage.py mycommand 2>&1 >> /tmp/mycron.log
* * * * * root /usr/bin/python /path/to/my/script/manage.py mycommand 2>&1 >> /tmp/mycron.log

In the first case, manage.py must be executable ( chmod +x manage.py ) and it's first line must be #!/usr/bin/python , or whatever your python interpreter path is. 在第一种情况下, manage.py必须是可执行文件( chmod +x manage.py ),并且其第一行必须是#!/usr/bin/python或任何python解释器路径。

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

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