greymd / tmux-xpanes

Awesome tmux-based terminal divider
MIT License
1.96k stars 61 forks source link

[feature request] flag (or method) to easily return console log to parent session #138

Closed FFdhorkin closed 4 years ago

FFdhorkin commented 4 years ago

Current usage looks like this in my bash script: xpanes -ss -c "docker build -t {} -f {}.Dockerfile ." "image1" "image2" "image3" "image4"

This works well enough, but what I'd really like to do is run them in the sub-shells, but when the xpanes command returns, print their logs in the terminal.

I know there's a --log option, but I don't care about preserving it on disk, just making sure the output isn't lost. I could use --log, then when I'm back to my original bash script, print the files to the console, but that feels... messy

greymd commented 4 years ago

If I understand what you would like to do, --stay option may solve your concern.

--stay keeps current terminal since new processes are going to be running on background. In your use case, --stay and -s (do not use -ss since it closes tmux windows automatically) can be used. If you run xpanes from outside of tmux session, use -S option also.

For example,

$ xpanes -S ./session --stay -s -c "docker build -t {} -f {}.Dockerfile ." "image1" "image2" "image3" "image4"

The new jobs are going to be running in background. You can check the progress of the jobs with this command.

$ tmux -S ./session attach

If the jobs are not finished, you can detach the session, wait for a while, and run above command again.

Please try this way with simple command like echo {}. If above workaround is not what you expected, please re-open this issue :)

FFdhorkin commented 4 years ago

If above workaround is not what you expected, please re-open this issue :)

I'm using xpanes for a Docker build script where I want to build six images simultaneously.

Xpanes is great for this, but the problem is that the build log gets lost when you hit enter (passing -s) or when done (passing -ss)

The workaround you've described is not ideal; it could be used to recover the state in the event that one of the builds failed, but it's also fairly likely that someone will forget and leave a tmux session running, and it doesn't output it to the original shell like I want. I also have no idea whether my coworkers are comfortable in tmux.

Something like this is what I want to do:

xpanes -s --log=foo --log-format="[:ARG:].log" -c "echo {}" "image1" "image2" "image3" "image4"
# then user hits enter
logfiles=(`find foo -name "*.log"`)
for filename in ${logfiles[@]}; do
    echo ================
    echo LOG FROM $filename:
    echo ================
    cat $filename ;
done
rm -rf foo

except I think it should be doable natively in xpanes: xpanes -s --cat_logs_when_exiting -c "echo {}" "image1" "image2" "image3" "image4"

greymd commented 4 years ago

I am highly appreciate your suggestion. But if I understand your background correctly, below command may solve your trouble. Could you try this one from terminal or your script?

xpanes -s -c "echo {} | tee >(cat) >$(tty)" "image1" "image2" "image3" "image4"

Below example is also works fine in my environment. sed is used to insert the argument name at the beginning of each line to distinguish each result.

xpanes -s -c "docker build -t {} -f {}.Dockerfile . | sed -u 's/^/{}:/' | tee >(cat) >$(tty)" "image1" "image2" "image3" "image4"
greymd commented 4 years ago

Oh sorry, above workaround does not work if xpanes starts from outside of tmux session.. If you are using xpanes on normal terminal, there is only messy solution..

$ rm -f /tmp/$$; xpanes -s -c "echo {} | tee >(cat) >>/tmp/$$" "image1" "image2" "image3" "image4" && cat
/tmp/$$

Let me figure out another workaround for a while. If there is no reasonable workaround, I will consider to implement this feature request.