How can I properly log output from a server running in a tmux session?

0
18
Asked By CuriousCoder99 On

I'm having trouble with a line in my server start script that uses tmux. I want to run the server command inside a new tmux session while also logging its output. Here's what I'm using:

tmux new-session -d -s ${TMUX_SESSION} ${SERVER_COMMAND} | tee -a ${LOG}

This starts the server correctly in tmux, and I can connect to the session to send commands using other scripts. The issue arises with the tee command not appending the server command's output to my log file; it seems to just append the output of the tmux new-session command, which is empty.

I tried using backticks around the server command and tee to capture output, but I got a 'command too long' error. Additionally, I've used tmux send-keys for the server command, which worked for logging but caused the tmux session to be non-persistent, so I couldn't review logs later. How can I structure this command properly so that I can log output and keep the session persistent?

4 Answers

Answered By TechieTina On

One approach is to redirect the output to a file instead of trying to capture it in tmux. Each command in a session doesn’t send its output directly to the client. You could also make the entire pipeline the command to run in the initial window of the session and then attach to it later.

Answered By TMUXGuru42 On

You need to run the tee command within the tmux session itself. The output of `tmux new-session` gets passed to tee instead of your server command. Try this:

tmux new-session -d -s ${TMUX_SESSION} "${SERVER_COMMAND} | tee -a ${LOG}"

If that doesn't work, you might want to add another shell:

tmux new-session -d -s ${TMUX_SESSION} bash -c "${SERVER_COMMAND} | tee -a ${LOG}"

Using quotes will ensure that the right command is executed in the session.

User123 -

I feel really stupid. I thought I tried this but I think I accidentally put the double quotes around the ${TMUX_SESSION} variable. The first option worked for me. Thank you!

Answered By ScriptingSage On

Why not use the pipe-pane command to log pane output? It's pretty handy for monitoring session output without extra complexity.

BashWhiz -

Pipe-pane would have worked if I had used send-keys to send it to the session. However, I prefer to keep it simple with one command to start the server and just use send-keys for additional commands.

Answered By BashWhiz On

Have you considered using send-keys? You could do something like:

tmux send-keys -t "session:1" 'tail -f $LOG' C-m

CuriousCoder99 -

I do use send-keys to send commands to the server, but I was facing issues where the tmux session would end. The solution from TMUXGuru42 with the double quotes worked out for my logging issue.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.