在 Ubuntu 14.04 中调用 Django 文件作为启动服务
Calling a Django file as a startup service in Ubuntu 14.04
我是这里的新手。我如何在我的 Django 应用程序中使用 python 文件,该文件将在我的 Ubuntu 服务器启动时使用。请帮助我。
谢谢
您可以按照 here 所述编写自己的管理命令,然后将其命名为 during/after boot,例如:
python /var/www/myproject/manage.py myscript somearg
命令可能如下所示:
from polls.models import Question
class Command(BaseCommand):
help = 'Delete stale questions'
def handle(self, *args, **options):
questions = Question.objects.filter(stale=True).delete()
如果你使用虚拟环境,请不要忘记事先激活你的虚拟环境,否则它将找不到 django 或其他 pip 包。
或者您使用提供 runscript
命令的第 3 方插件 django-extensions
python manage.py runscript delete_all_questions --script-args staleonly
from polls.models import Question
def run(*args):
questions = Question.objects.filter(stale=True).delete()
请参阅 here 了解有关如何添加脚本以在启动时自动运行的选项
我是这里的新手。我如何在我的 Django 应用程序中使用 python 文件,该文件将在我的 Ubuntu 服务器启动时使用。请帮助我。
谢谢
您可以按照 here 所述编写自己的管理命令,然后将其命名为 during/after boot,例如:
python /var/www/myproject/manage.py myscript somearg
命令可能如下所示:
from polls.models import Question
class Command(BaseCommand):
help = 'Delete stale questions'
def handle(self, *args, **options):
questions = Question.objects.filter(stale=True).delete()
如果你使用虚拟环境,请不要忘记事先激活你的虚拟环境,否则它将找不到 django 或其他 pip 包。
或者您使用提供 runscript
命令的第 3 方插件 django-extensions
python manage.py runscript delete_all_questions --script-args staleonly
from polls.models import Question
def run(*args):
questions = Question.objects.filter(stale=True).delete()
请参阅 here 了解有关如何添加脚本以在启动时自动运行的选项