从 Cron.d 运行 Python 脚本

发布于 2024-12-08 20:00:03 字数 1456 浏览 1 评论 0原文

我正在尝试通过安装在 Ubuntu 上 /etc/cron.d/mycron 的 crontab 运行 Django 管理命令。

我首先通过将以下内容写入 /etc/cron.d/mycron 来测试我的基本设置:

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

我确认 /tmp/mycron.log 每分钟更新一次并包含预期的文本。

然后,我使用实际管理命令尝试了以下变体:

* * * * * 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

即使生成了日志文件,它也不包含管理命令输出的预期日志文本。

每次变化后,我都会运行 sudo touch /etc/cron.d/ 来重新加载 cron。即使我的 Python 代码中有错误,我也希望记录某种错误消息。

我检查了 tail -f /var/log/syslog,它显示的唯一错误是不以“root”开头的 crontab,它给了我错误:

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

对于所有对于其他人,我看到类似的内容:

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

但是没有任何内容写入日志,并且我期望发生的数据库更改没有发生。我做错了什么?

I'm trying to run a Django management command via a crontab installed to /etc/cron.d/mycron on Ubuntu.

I first tested my basic setup by writing the following to /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.

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. Even if there was an error in my Python code, I'd expect some sort of error message to get logged.

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:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

昇り龍 2024-12-15 20:00:03

您需要的输出位于 /tmp/mycron.log 中,或邮寄到 root。从 root 下输入 mail 进行检查。

另外,你为什么要尝试不同的变化? crontab 文件的语法是严格定义的(man crontab -S 5),对于 /etc/crontab 来说是:

min hour day month weekday user cmdline

对于个人 crontab 来说是:

min hour day month weekday cmdline

You can't omit user name in /etc/crontab ,也不能使用 /usr/bin/python 代替用户名。

您也不需要 command 这个词,它之所以有效,是因为有一个具有该名称的内置 bash 函数,它显然只是从它的参数运行一个命令。

正确的行必须是以下任一行:

* * * * * 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

在第一种情况下,manage.py 必须是可执行的 (chmod +x manage.py),并且它的第一行必须是 #!/usr/bin/python,或者任何你的Python解释器路径。

The output you need is either in /tmp/mycron.log, or mailed to root. Type mail from under root to check.

Also, why are you trying different variations? The syntax of crontab files is strictly defined (man crontab -S 5), for /etc/crontab it is:

min hour day month weekday user cmdline

for personal crontabs, it is:

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.

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文