程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Using nohup to hang the background and start Django project under Linux

編輯:Python

django stay linux Up operation , Generally in xshell After remote connection , Start the service through the following command :

python manage.py runserver 0.0.0.0:8000

But there is a drawback , When the window closes, the service stops , How to make the window close without stopping the service ? That's it Background start !!!

and nohup Can achieve django The background operation is suspended when the project is started , Ensure that the closing of the window does not affect the operation of the service .

The specific operation is as follows :

step 1:cd Switch to django Of manage.py Catalog , Kill the previous before starting django manage.py Run process as . Method reference linux How to stop background operation Django project https://blog.csdn.net/qq_36564503/article/details/122628691 step 2: Then use nohup Start the process , You can specify the log output to django_8001.log, If not specified, the default is nohup.out. Specify the log path followed by 2>&1 &

(ablog) [[email protected] ablog.cn]# nohup python manage.py runserver 0.0.0.0:8001 >/www/wwwroot/ablog.cn/django_8001.log 2>&1 &
[1] 21616
(ablog) [[email protected] ablog.cn]# tail -f /www/wwwroot/ablog.cn/django_8001.log
nohup: ignoring input
Watching for file changes with StatReloader
[12/Feb/2022 01:30:41] "GET /news/ HTTP/1.1" 200 17423
[12/Feb/2022 01:30:42] "GET /static/js/news.js HTTP/1.1" 304 0
[12/Feb/2022 01:30:42] "GET /media/cache/14/90/14902925a4ccb040f4e40970abdf29c0.jpg HTTP/1.1" 304 0
[12/Feb/2022 01:30:42] "GET /media/cache/c6/a3/c6a365779473bcc68229d3b21576af55.jpg HTTP/1.1" 304 0

To view the real-time log, you can use tail -f djo.out

start.sh and stop.sh

Then it can be in manage.py Directory next start.sh File to start the django, Write another stop.sh Stop django service .
Use vim start.sh Edit the following 2 That's ok , After editing Esc sign out , Input :wq Save and exit

[[email protected] django2]# vim start.sh
ps -aux | grep python|xargs kill -9 # Note that if there are multiple virtual environments running multiple django The service needs to use ports
nohup python manage.py runserver 0.0.0.0:8000 >djo.out 2>&1 &

continue vim stop.sh edit , After editing Esc sign out , Input :wq Save and exit

ps -aux | grep python|xargs kill -9

After editing , Authorize this 2 File

[[email protected] django2]# chmod +777 start.sh
[[email protected] django2]# chmod +777 stop.sh

start-up django

Start the service execution ./start.sh, Close service execution ./stop.sh

After startup , Enter the address in the browser , And then execute tail -f djo.out You can see the real-time log


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved