从crontab运行Django命令,只能同时运行一个
我有一个Django“脚本”,每20秒运行一次 如下所示::(仅显示部分,工作正常;) 我的问题是,“命令脚本”有时可能需要20秒以上。要执行,我不想在上一个作业停止之前启动另一个作业从crontab运行Django命令,只能同时运行一个,django,crontab,django-commands,Django,Crontab,Django Commands,我有一个Django“脚本”,每20秒运行一次 如下所示::(仅显示部分,工作正常;) 我的问题是,“命令脚本”有时可能需要20秒以上。要执行,我不想在上一个作业停止之前启动另一个作业 如何解决这个问题?;) 您可以使用文件作为工作指标。(也许您也可以使用进程id,但使用文件的解决方案非常简单) 检查是否存在/tmp/iam_工作 如果不存在-创建它并运行脚本(如果存在-不执行任何操作) 作业结束后删除文件 在bash中: if [ ! -f /tmp/iam_working ] ; then
如何解决这个问题?;) 您可以使用文件作为工作指标。(也许您也可以使用进程id,但使用文件的解决方案非常简单)
if [ ! -f /tmp/iam_working ] ; then touch /tmp/iam_working; yourcommands; rm /tmp/iam_working; fi
您可以使用文件作为工作指示器。(也许您也可以使用进程id,但使用文件的解决方案非常简单)
if [ ! -f /tmp/iam_working ] ; then touch /tmp/iam_working; yourcommands; rm /tmp/iam_working; fi
您还可以使用数据库模型作为工作指标。它可以说比基于文件的方法更干净
# models.py
from django.db import models
class CommandStatus(models.Model):
running = models.BooleanField(default=False, null=False, blank=True)
从模型创建实例:
from myapp.models import CommandStatus
indicator = CommandStatus.objects.create()
在命令中使用实例:
status = CommandStatus.objects.all()[0]
if not status.running:
status.running = True
status.save()
# do stuff
status.running = False
status.save()
您还可以使用数据库模型作为工作指标。它可以说比基于文件的方法更干净
# models.py
from django.db import models
class CommandStatus(models.Model):
running = models.BooleanField(default=False, null=False, blank=True)
从模型创建实例:
from myapp.models import CommandStatus
indicator = CommandStatus.objects.create()
在命令中使用实例:
status = CommandStatus.objects.all()[0]
if not status.running:
status.running = True
status.save()
# do stuff
status.running = False
status.save()