如何使用bash脚本退出Django开发服务器?

2024-05-20 17:10:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了一个bash脚本来启动一个Django开发服务器,但是,我希望在服务器运行bash脚本时也能够退出服务器。我正在为Koding.com编写一个web应用程序,它在一个连接到用户个人虚拟机的在线终端上,通过按下按钮运行bash脚本来启动Django进程,我希望用户也可以按下按钮来结束该进程。我知道控件C会结束这个过程,但是我还没有在bash脚本中找到如何完成这个过程。我怎么能这样做呢?谢谢! enter image description here

更新:我最终将这个问题的选择答案和我问的另一个问题(如何在django shell中运行unix命令)的答案结合起来,以解决我的问题:https://stackoverflow.com/a/22777849/2181017


Tags: django答案用户服务器脚本combashweb
3条回答

GNU屏幕呢?http://www.gnu.org/software/screen/

screen
./manage.py runserver 0.0.0.0:8000
ctrl + a then d (this will detach from screen)
exit (from the shell)

当你需要停止时,重新登录shell

screen -r
ctrl + c
exit (from screen)
exit (from the shell)

把这个放在~/.screenrc中可以得到一个很好的屏幕状态行

hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{=kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B}%Y-%m-%d %{W}%c %{g}]'

您可以终止在8000/TCP上侦听的进程:

fuser -k 8000/tcp

fuser命令显示哪些进程正在使用命名文件、套接字或文件系统,-k选项也会杀死它们。

Wait, just to clarify, there's no way to temporarily exit that django shell thing so that I could run a unix command?

启动开发服务器时,它将在前台运行,并阻止提示。您可以通过点击CTRL+Z暂停在前台运行的任务,然后运行bg命令将任务发送到后台(我假设您正在运行bash shell或类似的命令)。jobs命令将列出暂停的任务或后台运行的任务。您可以使用fg命令将任务置于前台。

请参阅bash手册页(man bash)。

您只需使用CTRL+C退出服务器即可。
否则你可以用ubuntu系统做如下的事情。

prashant@prashant-OptiPlex-3010:~$  ps -eaf|grep runserver
prashant  3445  2805  4 11:20 pts/0    00:00:00 python manage.py runserver
prashant  3446  3445  5 11:20 pts/0    00:00:00 /usr/bin/python manage.py runserver
prashant  3449  3375  0 11:20 pts/1    00:00:00 grep --color=auto runserver
prashant@prashant-OptiPlex-3010:~$ kill 3446
prashant@prashant-OptiPlex-3010:~$ 

相关问题 更多 >