是否有其他方法python manage.py crontab运行在django服务器?



我在django中使用djjango_crontab包创建了一个定时作业。我已经增加了这项工作。它成功添加,但它只得到执行,每当我运行python manage.py crontab运行daeea9e88c171494b1610bdebfasd123不与runserver命令。这是我的看法。

cron.py文件
def my_scheduled_job():
file = open('geek.txt','w')
now = timezone.now()
file.write("This is the write command")
file.write("It allows us to write in a particular file")
file.write(str(now))
file.close()
print(now)
return True

settings.py

CRONTAB_LOCK_JOBS = False
CRONTAB_DJANGO_PROJECT_NAME = 'api' 
CRONTAB_DJANGO_SETTINGS_MODULE = 'api.settings'
CRONJOBS = [
('*/1 * * * *', 'app.cron.my_scheduled_job')
]

我想要的是它的任务每分钟执行一次,每当我运行python manage.py runserver。有遗漏的东西吗?请帮我解决。

我也有同样的问题,现在已经解决了。我只需要在。env文件中设置我的用户环境变量,问题就解决了。

我的虚拟环境路径:/home/nasir/Work//dj_venv/bin/python3.8

我的cronjob运行路径:path/home/nasir/Work/django_projects_neosoft/e_commerce/manage.py crontab run 6ef9e6b4bdce09989e8ed084d2ffc2bb

我的系统的完整路径:/home/nasir/work/django_projects_neosoft/dj_venv/bin/python3.8/home/nasir/work/django_projects_neosoft/e_commerce/manage.py crontab run 6ef9e6b4bdce09989e8ed084d2ffc2bb

我在终端中粘贴了完整的路径,得到了与数据库和环境变量相关的错误。我在.env文件中设置了环境变量,问题就解决了。Cronjob运行正常

在你的情况下,你可能会得到一些其他的错误,尝试解决它,至少你知道问题在哪里。

最新更新