So I am using this tmux plugin for logging the entire tmux history. https://github.com/tmux-plugins/tmux-logging
The plugin uses the key bindings C-b + P to start and stop the logging session. Basically, prefix + P.
I am programatically trying to to stop and start the logging session to create a new output file for each day. For this, I want to setup a crontab to run a simple script. The content of the script is as follows.
tmux send-keys -t 0:0 C-b P # stops the logging session
tmux send-keys -t 0:0 C-b P # restarts the logging session
But when I attach to the tmux session, what I see is this printed on the screen.
^BP^BP
So C-b instead of being treated as a prefix, is being treated as a normal key combination. I have also tried using the following, but still the same result.
tmux send-prefix -t 0:0 # prints ^B on the session window
tmux send-keys -t 0:0 P
What is the workaround for this?
According to the maintainer of tmux, "You can't trigger tmux key bindings with send-keys. You could just run the commands the key is bound to instead.".
For your specific case, I see in logging.tmux:
tmux bind-key "$logging_key" run-shell "$CURRENT_DIR/scripts/toggle_logging.sh"
tmux bind-key "$pane_screen_capture_key" run-shell "$CURRENT_DIR/scripts/screen_capture.sh"
tmux bind-key "$save_complete_history_key" run-shell "$CURRENT_DIR/scripts/save_complete_history.sh"
tmux bind-key "$clear_history_key" run-shell "$CURRENT_DIR/scripts/clear_history.sh"
where $CURRENT_DIR is the directory that this script lives in.
So, find that script on your machine and then change your send-keys to
tmux send-keys -t 0:0 '$SCRIPT_DIR/scripts/toggle_logging.sh' Enter
Related
In my normal shell I create tmux sessions for running in the background with
tmux new-session -d -s my-session my-script.sh (1)
Now I want to add logging of all output via pipe-pane to the session. I know how to do this in a not-detached session (when being inside tmux):
Ctrl-B : pipe-pane -o 'cat >>~/tmp/output.log' (2)
But how can I tell tmux on creation of a detached session -- via (1) -- to add straight away the pipe-pane tmux command?
I basically look for a way in my normal shell to create detached tmux sessions with logging. - I am using tmux 1.6.
You can always combine the two commands into one tmux command (I dont have tmux 1.6 to test this on but it works for 2.2):
tmux new-session -d -s my-session my-script.sh \; pipe-pane -o 'cat >>~/tmp/output.log'
If you were using tmux 2.9 or later you can set a hook to run a command when a new session starts. Put in your ~/.tmux.conf
set-hook -g session-created "pipe-pane -o 'cat >>~/tmp/output.log'"
To cope with many sessions, you might include the session name in the filename, e.g. output.#{session_name}.log.
I'm having several issues with tmux on my new account but the most bizarre one is this: what I write in my tmux configuration file is read by bash (or the terminal, whatever). My ~/.tmux.conf is quite small:
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
This has the effect of disabling h,j,k,l in the terminal (k doesn't work like up, so I suppose it doesn't actually read the configs per se). My .bashrc is quite small as well (apart from the default Ubuntu stuff):
alias ls='ls --group-directories-first --color'
alias tmux='tmux new-session \; split-window -h\;'
source ~/dir/fileNotRelatedToThis
Removing the entries in .tmux.conf solves the problem, but I have those settings for a reason and have never experienced anything like this problem before. Anyone who can think of a real solution?
Oh and removing .tmux.conf does not solve the problem, so currently I don't have h,j,k,l in the terminal, hm...
edit: Rebooting enabled the letters again, phew...
This will work for setting ctrl + key_press.
bind-key -n C-h select-pane -L
bind-key -n C-l select-pane -R
bind-key -n C-j select-pane -D
bind-key -n C-k select-pane -U
You also need to attach to the session something like this should work:
tmux new-session -d -n Dropdown -s Command;
tmux splitw -h -p 50 -t 0
tmux attach-session -t Command
I have a process I want to monitor by tail -f on several output files in different directories. I can use a bash script start tmux as a detached session, create multiple panes, change to the top directory and reattach. This all works. My problem comes when I want the script to send more commands later. Is there some reason why once I attach, my script can't send commands or detach/reattach later? The reason to do more commands is that some files take 45 seconds to be created before I can tail them.
My example looks like
#!/bin/bash
# this depends on some settings from my ~/.tmux.conf
TopLevel='/tsload'
SimDir=`ls -d $TopLevel/SIM_ISS*`
# create and detach session
tmux new-session -s simwatch -n Sim_Watch -d
# make left & right panes, only 1 window
tmux split-window -h -t simwatch
# change to toplevel dir
tmux send-keys -t simwatch:1.1 "cd $SimDir" C-m
tmux send-keys -t simwatch:1.2 "cd $SimDir" C-m
tmux attach -t simwatch
At this point my script fails when I try to have it do more. I've also tried 'tmux detach -t simwatch' issue commands and reattach but they don't take effect.
Not very familiar with tmux, but from my experiment with your script, it looks like that tmux 'attach -t simwatch' is a blocking operation, i.e. it starts and will end once you detach/exit from the session, which is when the script will continue.
Regarding the issue with the files that appear later, if you know their paths, you can try to follow them with "tail -F", which will wait for the files to appear.
tail --follow=name --retry missing_file
I'd like to start tmux from terminal, split it window and execute in the splits some commands. Furthermore, I'd like to did with bash script, that I start from terminal. Now, I've been able to start tmux only.
#!/bin/sh
tmux
# How to split tmux window there, and start commands in splits?
There is a script started tmux, splitted for to panes and started ls in the left pane and top in the right one.
#!/bin/sh
tmux new-session -d -s main ;
tmux split-window -h ;
tmux select-pane -L
tmux send-keys -t main 'ls' C-m
tmux select-pane -R
tmux send-keys -t main 'top' C-m
tmux attach-session -d -t main
I have a script (.sh) and I want it to run in a existing tmux session. I have 1 session with 8 windows in.
Is there a command like tmux a -t session-name, which also specify the window?
And would a script like this work?
#!/bin/bash
tmux a -t session-name #What ever to write to specify window# java -jar -Xmx4G -Xms4G Spigot.jar
You can change the active window of a session before you attach to the session.
tmux -t <session-name> select-window -t <windowID>
tmux a -t <session-name>
You can combine two tmux commands as well.
tmux -t session-name select-window -t <windowID> \; a
If you really want to run java, presumably you want to create a new window with new-window, rather than select an existing one with select-window.
Newer versions of tmux (at least 1.9; did the above ever work, perhaps in 1.6?) no longer appear to have a -t option to specify the session to apply commands to. Instead, each individual command specifies the session.
tmux select-window -t <session-name>:<windowID> \; a -t <session-name>
For tmux version 2.1 this works
tmux a -t <session-name> \; select-window -t <windowID> \;
You can specify the window after the session separated by a colon.
tmux a -t session:window
You can even attach to a specific pane.
tmux a -t session:window.pane
Pane can be a number starting from 0. Window can be a number or name.
man tmux has more info about different syntaxes allowed for target-session, target-window, and target-pane.
target-window (or src-window or dst-window) specifies a window in the form session:window...
This syntax works on any other command like send-keys. If it's not working you may be on an older version of tmux and need to upgrade or try an approach suggested in the other answers.