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
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 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