如何从tmux会话中获取stdout和stderr? [英] How to get stdout and stderr from a tmux session?
问题描述
我正在linux系统中编写示例python程序.我正在使用 tmux 创建会话并在tmux会话中执行另一个脚本.我想从tmux会话中将stdout和stderr移到父脚本,但是以某种方式不起作用.
I am writing a sample python program in linux system. I am using tmux to create a session and execute another script within the tmux-session. I would like to get the stdout and stderr out of the tmux session to the parent script but that somehow does not work.
代码段:
cmd = "tmux new-session -d -s 'test' '/my_dir/fibonacci.py __name:=fibonacci_0'"
proc = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
(stdout, stderr) = proc.communicate()
print(stderr)
我遇到了使用 show-buffer 和
I have came across answers to use show-buffer and pipe-pane. But that did not help. Maybe I need to modify the tmux command itself.
推荐答案
感谢您的支持.经过一番研究后,我想出了一种解决方法.我只是在这里为有类似需求的人添加它.
Thank you for your support. After digging a bit, I came up with a workaround. I am just adding it here for someone with similar needs.
我所做的是创建一个命名管道,将tmux会话的输出重定向到命名管道,然后最终从中读取.
What I have done is created a named pipe, redirect the output of tmux session to named pipe and then finally read from it.
# this attach if a session exists or creates one, then exits from the session
call("tmux new-session -A -s test \; detach", shell=True)
# to avoid conflict, remove existing named pipe and then create named pipe
call("rm -f /tmp/mypipe && mkfifo /tmp/mypipe && tmux pipe-pane -t test -o 'cat > /tmp/mypipe'", shell=True)
# feed the pipe to the stdout and stderr
poc = Popen(['cat', '/tmp/mypipe'], stdout=PIPE, stderr=PIPE)
# finally execute the command in tmux session
Popen(['tmux', 'send-keys', '-t', '/my_dir/fibonacci.py', 'C-m'])
(stdout, stderr) = proc.communicate()
print(stderr)
希望这会有所帮助.
这篇关于如何从tmux会话中获取stdout和stderr?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!