如何将SimpleSocket服务器移至后台进程 [英] How to move SimpleSocket server into a background process

查看:121
本文介绍了如何将SimpleSocket服务器移至后台进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 socketServer ,可以在主线程上完美运行。

I have a simple socketServer that works perfectly on the main thread.

#Server PORT
PORT = 8020
#reassign variables
Handler = Server #this is a SimpleHTTPHandler

httpd = SocketServer.TCPServer(("", PORT), Handler)

httpd.serve_forever()

我需要使它在后台运行,并能够随意停止该过程。正确的方法是什么?

I need to have this run in the background and have the ability to stop the process at will. What is the proper way to do this?

编辑

对不起,我当时是不清楚。我需要让服务器不间断运行,并且只能从SSH访问系统,所以我不能只是启动它而走开。

Sorry I was unclear. I need to have the server running non stop and I can only access the system from SSH so I can't just start it and walk away.

推荐答案

假设您正在POSIX操作系统上运行脚本,并且脚本名为 socket_server.py ,则可以使用 nohup 像这样:

Assuming you are running your script on a POSIX operating system and your script is named socket_server.py, you can use nohup like this:

$ nohup python socket_server.py >> /dev/null 2>&1 &

这会将脚本置于后台,使其不受挂断的影响,并且可以退出SSH会议。外壳程序将打印出作业号和PID:

That will put your script in the background, make it immune to hangups, and you can exit your SSH session. The shell will print out the job number and PID:

$ [1] 1234

您可以稍后使用 kill 发送SIGTERM来停止它:

You can stop it later by getting sending a SIGTERM using kill:

$ kill -SIGTERM 1234

这篇关于如何将SimpleSocket服务器移至后台进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆