如何使用 bash 脚本退出 Django 开发服务器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22775618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I quit a Django development server with a bash script?
提问by Regnarg
I wrote a bash script to start a Django development sever, however, I want to be able to quit the server while it's running with a bash script as well. I'm writing a web app for Koding.com that starts a Django process in an online terminal linked to the user's personal VM by running a bash script with a press of a button, and I want users to be able to end the process with a press of a button as well. I know that control C would end the process, but I haven't been able to find out how to do that in a bash script. How could I go about doing this? Thanks!
我编写了一个 bash 脚本来启动 Django 开发服务器,但是,我希望能够在服务器运行 bash 脚本时退出服务器。我正在为 Koding.com 编写一个 Web 应用程序,该应用程序通过按下按钮运行 bash 脚本,在链接到用户个人 VM 的在线终端中启动 Django 进程,我希望用户能够结束该进程按下按钮也是如此。我知道控制 C 会结束这个过程,但我一直无法在 bash 脚本中找到如何做到这一点。我怎么能去做这件事?谢谢!
UPDATE: I ultimately combined my selected answer for this question and this answer from another question I asked (how to run unix commands in django shell) to solve my problem: https://stackoverflow.com/a/22777849/2181017
更新:我最终结合了我为这个问题选择的答案和我问的另一个问题的答案(如何在 django shell 中运行 unix 命令)来解决我的问题:https: //stackoverflow.com/a/22777849/2181017
回答by Paulo Scardine
You can kill the process listening on 8000/TCP:
您可以终止侦听 8000/TCP 的进程:
fuser -k 8000/tcp
The fuser
command shows which processes are using the named files, sockets, or filesystems and the -k
option kills them as well.
该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?
等等,只是为了澄清一下,没有办法暂时退出那个 django shell 以便我可以运行 unix 命令吗?
When you start the development server it will run in foreground, blocking the prompt. You can pause the task that is running in foreground by hitting CTRL+Z
, and then send the task to background running the bg
command (I assume you are running the bash shell or a lookalike). The jobs
command will list paused tasks or tasks running in background. You can bring a task to foreground using the fg
command.
当您启动开发服务器时,它将在前台运行,阻止提示。您可以通过点击 暂停在前台运行的任务,CTRL+Z
然后将任务发送到后台运行bg
命令(我假设您正在运行 bash shell 或类似的)。该jobs
命令将列出暂停的任务或在后台运行的任务。您可以使用该fg
命令将任务置于前台。
Please refer to the bash man page (man bash
).
请参阅 bash 手册页 ( man bash
)。
回答by sherpya
what about GNU screen? http://www.gnu.org/software/screen/
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)
when you need to stop, login back in the shell
当您需要停止时,重新登录 shell
screen -r
ctrl + c
exit (from screen)
exit (from the shell)
put this in ~/.screenrc
for a nice screen statusline
把它放在~/.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}]'
回答by pygaur
You can simply use CTRL+C
to quit your server .
else with ubuntu system you can do like below .
您可以简单地使用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:~$