【发布时间】:2017-11-30 21:06:06
【问题描述】:
我有一个 bash 脚本,它在后台运行一些 python 脚本。我已经给它们附加了一个中断信号,所以当我按 ctrl-c 时,我会从它们中逃脱。但是,我的 python 脚本启动了一个 SimpleHTTPServer。中断会终止 python 脚本,但不会终止 SimpleHTTPServer。我将如何结束该过程?
我在main-script.sh 中有以下外壳:
trap 'kill %1; kill %2' SIGINT
cd "$DIR_1" && ./script1.py &
cd "$DIR_2" && ./script2.py &
./script3.py
python 脚本只是启动了一个带有一些额外标头的 SimpleHTTPServer。
在终端中运行ps x 会给出
60958 pts/1 S 0:00 /bin/bash ./main-script.sh
60959 pts/1 S 0:00 /bin/bash ./main-script.sh
60960 pts/1 S 0:02 /usr/bin/python ./script1.py host:port1
60962 pts/1 S 0:01 /usr/bin/python ./script2.py host:port2
在 ctrl-c 之后
60960 pts/1 S 0:02 /usr/bin/python ./script1.py host:port1
60962 pts/1 S 0:01 /usr/bin/python ./script2.py host:port2
这里编辑的是启动服务器的主要代码:
#!/usr/bin/python
import SimpleHTTPServer
import sys
from SimpleHTTPServer import SimpleHTTPRequestHandler
import BaseHTTPServer
def test(HandlerClass=SimpleHTTPRequestHandler,
ServerClass=BaseHTTPServer.HTTPServer):
protocol = "HTTP/1.0"
server_address = (host, port)
HandlerClass.protocol_version = protocol
httpd = ServerClass(server_address, HandlerClass)
httpd.serve_forever()
if __name__ == '__main__':
test()
任何帮助将不胜感激。谢谢你。
【问题讨论】:
-
包括启动服务器的代码,这取决于它
-
@ArtemBernatskyi 添加了代码片段。
标签: python bash shell simplehttpserver