首先,您要根据本指南创建一个管理命令。
https://docs.djangoproject.com/en/2.1/howto/custom-management-commands/
说我们想经营
closepoll
示例中的命令每5分钟执行一次。
然后需要创建脚本来运行此命令。
Linux/MacOS:
#!/bin/bash -e
cd path/to/your/django/project
source venv/bin/activate # if you use venv
python manage.py closepoll # maybe you want to >> /path/to/log so you can log the results
将文件存储为
run_closepoll.sh
运行
chmod +x run_closepoll.sh
在命令行中
现在我们可以使用crontab运行命令
运行
crontab -e
在您的命令行中
添加此行:
*/5 * * * * /path/to/run_closepoll.sh
现在命令将每5分钟运行一次。
如果你不熟悉crontab,你可以使用这个网站
https://crontab-generator.org/
窗户:
Same content as the above example, but remove the first line and save as run_closepoll.bat
在“开始”菜单中,搜索
Task Scheduler
,按照gui上的说明操作,从那里开始应该很简单。
有关任务计划程序的详细信息,请参见此处:
https://docs.microsoft.com/en-us/windows/desktop/taskschd/using-the-task-scheduler