我有一堆脚本,用于在多个服务器上启动类似的进程。我想将它们浓缩为一个名为“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/

10-12 00:24
查看更多