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
可以看到实时日志