gpt4 book ai didi

python - 调用通过 ssh 创建子进程的 Python 脚本挂起

转载 作者:行者123 更新时间:2023-11-28 19:27:10 24 4
gpt4 key购买 nike

我有一堆脚本,用于在多个服务器上启动类似的进程。我想将它们压缩成一个名为“START”的 Python 脚本,但是当它通过 ssh 运行时会发生一些奇怪的事情。

$ ./START APP_A 按预期工作:APP_A 启动并开始执行它的操作。控制立即返回到控制台(在 APP_A 终止之前)。

$ ssh localhost/path_to/START APP_A 类似的工作:APP_A 启动并开始做它的事情,但 ssh 不会在屏幕上打印任何输出或将控制返回到控制台,直到之后APP_A 终止。

我认为这是信号或文件句柄的问题,但我不知所措。这是导致问题的 Popen 调用:

sub = subprocess.Popen(shlex.split(cmd), stdout=open(file_out, 'a+'), stderr=subprocess.STDOUT, close_fds=True)
print 'New PID:', sub.pid

我在 RHEL 上使用 Python 2.4.3。

编辑:包装 Python 脚本似乎可行:

DIR="$( cd "$( dirname "$0" )" && pwd )"
pushd $DIR >> /dev/null
./START $1 &
popd >> /dev/null

最佳答案

不要使用 shlex 与 subprocess 一起调用。 It doesn't do what you expect .相反,给子进程一个命令的 Python 列表,比如

subprocess.Popen(['/some/program', 'arg1', 'arg2', 'arg3'])

关于python - 调用通过 ssh 创建子进程的 Python 脚本挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7571796/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com