首页 > 解决方案 > Django cronjob 没有运行

问题描述

我正在尝试从自定义 Django 命令创建一个 cronjob。当我手动运行命令时,它运行良好。正是我想要的。唯一的事情是,我希望它安排好(一天两次)。但是当我输入完全相同的命令时crontab -e它不起作用?有什么建议么?

crontab -e:

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
* * * * * /usr/bin/python3 /home/marvin/VulnScanner/manage.py pentest 3

仅运行此命令可以正常工作:

/usr/bin/python3 /home/marvin/VulnScanner/manage.py pentest 3

标签: djangocron

解决方案


好的,我找到了解决方案。crontab 实际上正在运行(正如我所说的那样),因为 te log 是这样说的。

但是我在我的 python 文件中使用了一个可执行文件,而 crontab 找不到。这必须与默认情况下 crontab 没有 PATH 的情况有关。用户 shell 会在路径中查找可执行文件。crontab 没有。您可以通过在 crontab 顶部添加来解决此问题:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

我已将此附加到我的 cronjob 中,以获取我的 python 脚本的输出:

 >> /home/user/cronlog.log 2>&1

推荐阅读