开发者

Calling Python script that creates a subprocess over ssh hangs

I have a bunch of scripts that are used to start similar processes across a number of servers. I'd like to condense them down to one Python script called 'START', but something weird is happen开发者_开发问答ing when it's run over ssh.

$ ./START APP_A works as expected: APP_A is launched and begins doing its thing. Control is returned to the console immediately (before APP_A terminates).

$ ssh localhost /path_to/START APP_A sort of works: APP_A is launched and begins doing its thing, but ssh doesn't print any output to the screen or return control to the console until after APP_A terminates.

I assume it's a problem with signals or file handles, but I'm at a loss. Here's the Popen call that seems to be causing the trouble:

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

I'm using Python 2.4.3 on RHEL.

EDIT: Wrapping the Python script seems to work:

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


Don't use shlex to call in conjunction with subprocess. It doesn't do what you expect. Instead, give subprocess a Python list of the command, like

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


When you do:

ssh some_host remote_command remote_cmd_param

then it is normal that ssh does not return control before remote_command is done. If you want otherwise you need to sent it to background aby adding & at the end.

ssh redirects stdout of remote_command to its (local) stdout. If you don't see any output this may be because remote_command does not set any to stdout but tries to send it to the console for example. This is why you cannot do:

ssh remote_host mc # or any other command using terminal


You should put this in START_APP_A

nohup /path/to/APP_A >/path/to/log 2>&1 </dev/null &

Then it will work, and all the output from APP_A will go into a log file which you can inspect when you need to.

Note that if you need to inspect this output while APP_A runs, then you need to change APP_A so that it flushes stdout after printing or else change stdout to be unbuffered.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜