15

I can run a command in a detached tmux session with:

tmux new -d 'while true; do date; sleep 1; done'

I can run a command in an attached tmux session and log the console output with:

tmux new 'while true; do date; sleep 1; done' \; pipe-pane 'bzip2 > /tmp/log.bz2'

But how can I run a command in a detached tmux session AND log the console output? The following runs a command in a detached tmux session, but silently fails to log the output:

tmux new -d 'while true; do date; sleep 1; done' \; pipe-pane 'bzip2 > /tmp/log.bz2'

The closest I've been able to get is the following, which starts the command in a non-detached tmux, starts logging, and then detaches:

tmux new 'while true; do date; sleep 1; done'  pipe-pane 'bzip2 > /tmp/log.bz2' \; detach

This works, but prints the following gibberish onto the console after:

^[[?62;9;c

What is the right way of doing this?

1
  • You'd probably need to include the piping to pipe-pane in the specific command you're asking tmux to execute. I'm not really familiar with tmux, but I would start by ignoring pipe-pane and just get regular output redirection working, then move into tmux, then add pipe-pane magic. One step at a time. Also be mindful of which shell sees what command line (you may need double escaping). In addition, tee may be your friend while figuring it all out.
    – user
    Jun 13, 2015 at 18:45

2 Answers 2

9

Hopefully by now you have found an answer to this oddness. Here is what I found:

While using tmux 1.8 (default on centos 7) I was experiencing the same issue as you. So instead I removed that version and compiled from source version 2.2 of tmux.

As of this version the following works without weird characters:

tmux new -d 'while true; do date; sleep 1; done' \; pipe-pane 'cat > /tmp/log'

So basically it must have been an issue with older versions. I am unaware of which version corrected the issue/feature but I know 2.2 works like a charm.

Be aware, the log will contain some control characters. Like ^C, when exiting the while loop.


I know this is an old question but since this is a fairly high result in google when looking for tmux detached logging and I ran into the same issue figured might as well leave my solution here for anyone that could be helped by it.

2

You can run a script in a detached tmux sessions and log both stdout and stderr to a file, with something like this:

tmux new -d 'script.sh |& tee tmux.log'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .