Run command in detached tmux session and log console output to file

Solution 1:

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.

Solution 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'