zoukankan      html  css  js  c++  java
  • Linux学习20-nohup挂后台启动django

    前言

    django在linux上运行,一般在xshell远程连接后,是通过python manage.py runserver 0.0.0.0:8000启动服务。但是这样有个弊端,窗口关闭服务就停止了。
    nohup可以启动的时候挂后台运行

    nohup后台运行

    cd到django的manage.py目录,启动之前先杀掉进程

    [root@yoyo ~]# cd /usr/local/django2/
    [root@yoyo django2]# ps -aux | grep python|xargs kill -9
    kill: cannot find process "root"
    kill: sending signal to 21029 failed: No such process
    kill: cannot find process "0.0"
    kill: cannot find process "0.0"
    kill: sending signal to 112660 failed: No such process
    kill: sending signal to 968 failed: No such process
    kill: cannot find process "pts/2"
    kill: cannot find process "S+"
    kill: cannot find process "14:32"
    kill: cannot find process "0:00"
    kill: cannot find process "grep"
    kill: cannot find process "--color=auto"
    kill: cannot find process "python"
    [root@yoyo django2]# 

    接着使用nohup启动进程,可以指定日志输出到djo.out,如果不指定默认是在nohup.out。指定日志路径后面加上2>&1 &

    [root@yoyo django2]# nohup python manage.py runserver 0.0.0.0:8000 >djo.out 2>&1 &
    [1] 21616
    [root@yoyo django2]# tail -f djo.out 
    nohup: ignoring input
    [25/Mar/2019 14:36:31] "GET / HTTP/1.1" 200 24
    [25/Mar/2019 14:36:37] "GET / HTTP/1.1" 200 24
    [25/Mar/2019 14:36:38] "GET / HTTP/1.1" 200 24
    [25/Mar/2019 14:36:39] "GET / HTTP/1.1" 200 24

    查看实时日志可以用tail -f djo.out

    start.sh和stop.sh

    接着可以在manage.py目录下一个start.sh文件启动django,再写个stop.sh停掉django服务。
    使用vim start.sh编辑以下2行,编辑完成后Esc退出,输入:wq保存退出

    [root@yoyo django2]# vim start.sh
    
    ps -aux | grep python|xargs kill -9
    nohup python manage.py runserver 0.0.0.0:8000 >djo.out 2>&1 &
    

    继续使用vim start.sh编辑,编辑完成后Esc退出,输入:wq保存退出

    ps -aux | grep python|xargs kill -9
    

    编辑完成之后,授权这2个文件

    [root@yoyo django2]# chmod +777 start.sh
    
    [root@yoyo django2]# chmod +777 stop.sh

    启动django

    启动服务执行./start.sh,关闭服务执行./stop.sh

    启动完成后,在浏览器输入地址,然后执行tail -f djo.out可以看到实时日志

  • 相关阅读:
    获取aspx页面执行时间完全解决方案
    WebForm中DataGrid的20篇经典文章
    不走寻常路 设计ASP.NET应用程序的七大绝招
    动态绑定dropdownlist 开始拣.NET
    Notes中几个处理多值域的通用函数
    Lotus开发之Lotus Notes中域的验证
    Undokumentierte @Formeln/LotusScript im Lotus Notes Client/Server
    domino server端的Notes.ini详解
    Lotus开发基本性能优化
    以Ajax方式显示Lotus Notes视图的javasript类库NotesView2
  • 原文地址:https://www.cnblogs.com/mashuqi/p/10955369.html
Copyright © 2011-2022 走看看