fix: 修复 Windows 后台运行失败的问题
- daemonize() 使用 python -m 方式启动子进程,避免 console_scripts 入口点路径问题 - daemonize() 在 Popen 后调用 sys.exit(0) 让父进程退出 - 添加 --_daemon_child 隐藏参数,子进程跳过 daemonize 直接运行服务 - 将 _run_daemonized 重命名为 _run_child,职责更清晰
This commit is contained in:
@@ -30,6 +30,7 @@ def build_parser():
|
|||||||
p_start = sub.add_parser("start", help="Start the proxy daemon")
|
p_start = sub.add_parser("start", help="Start the proxy daemon")
|
||||||
p_start.add_argument("--foreground", action="store_true", help="Run in foreground (no daemon)")
|
p_start.add_argument("--foreground", action="store_true", help="Run in foreground (no daemon)")
|
||||||
p_start.add_argument("--port", type=str, default=None, help="Override listen port")
|
p_start.add_argument("--port", type=str, default=None, help="Override listen port")
|
||||||
|
p_start.add_argument("--_daemon_child", action="store_true", help=argparse.SUPPRESS)
|
||||||
|
|
||||||
# stop
|
# stop
|
||||||
sub.add_parser("stop", help="Stop the proxy daemon")
|
sub.add_parser("stop", help="Stop the proxy daemon")
|
||||||
@@ -67,8 +68,11 @@ def cmd_start(args):
|
|||||||
|
|
||||||
if args.foreground:
|
if args.foreground:
|
||||||
_run_foreground(config_dict)
|
_run_foreground(config_dict)
|
||||||
|
elif args._daemon_child:
|
||||||
|
_run_child(config_dict)
|
||||||
else:
|
else:
|
||||||
_run_daemonized(config_dict)
|
daemonize()
|
||||||
|
_run_child(config_dict)
|
||||||
|
|
||||||
|
|
||||||
def _run_foreground(config_dict):
|
def _run_foreground(config_dict):
|
||||||
@@ -85,7 +89,7 @@ def _run_foreground(config_dict):
|
|||||||
server.server_close()
|
server.server_close()
|
||||||
|
|
||||||
|
|
||||||
def _run_daemonized(config_dict):
|
def _run_child(config_dict):
|
||||||
from logging.handlers import RotatingFileHandler
|
from logging.handlers import RotatingFileHandler
|
||||||
|
|
||||||
log_path = get_log_path()
|
log_path = get_log_path()
|
||||||
@@ -97,8 +101,6 @@ def _run_daemonized(config_dict):
|
|||||||
root.setLevel(logging.INFO)
|
root.setLevel(logging.INFO)
|
||||||
root.addHandler(handler)
|
root.addHandler(handler)
|
||||||
|
|
||||||
daemonize()
|
|
||||||
|
|
||||||
pid = os.getpid()
|
pid = os.getpid()
|
||||||
write_pid(pid)
|
write_pid(pid)
|
||||||
|
|
||||||
|
|||||||
@@ -111,14 +111,16 @@ def daemonize():
|
|||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
creation_flags = subprocess.CREATE_NEW_PROCESS_GROUP | subprocess.DETACHED_PROCESS
|
creation_flags = subprocess.CREATE_NEW_PROCESS_GROUP | subprocess.DETACHED_PROCESS
|
||||||
|
cmd = [sys.executable, "-m", "jianda_proxy"] + sys.argv[1:] + ["--_daemon_child"]
|
||||||
subprocess.Popen(
|
subprocess.Popen(
|
||||||
[sys.executable] + sys.argv + ["--_daemon_child"],
|
cmd,
|
||||||
creationflags=creation_flags,
|
creationflags=creation_flags,
|
||||||
close_fds=True,
|
close_fds=True,
|
||||||
stdin=subprocess.DEVNULL,
|
stdin=subprocess.DEVNULL,
|
||||||
stdout=subprocess.DEVNULL,
|
stdout=subprocess.DEVNULL,
|
||||||
stderr=subprocess.DEVNULL,
|
stderr=subprocess.DEVNULL,
|
||||||
)
|
)
|
||||||
|
sys.exit(0)
|
||||||
else:
|
else:
|
||||||
pid = os.fork()
|
pid = os.fork()
|
||||||
if pid > 0:
|
if pid > 0:
|
||||||
|
|||||||
Reference in New Issue
Block a user