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.
Related
I have a program on the server, which execution should be initiated from the client by bash script with the use of ssh. Moreover, this script should open the tmux session on the server, run the program with some argument and terminate the session after program return.
I tried several solutions, but no one has been successful.
1)
#! /bin/bash
argument='12345678'
ssh user#host << EOF
tmux new-session -t session1
./program $argument
tmux kill-session -t session1
EOF
The program executes on the server, but without tmux session.
Output: "open terminal failed: not a terminal"
2)
#! /bin/bash
ssh user#host -t 'tmux new-session -t session1'
As minimum, this command open the session (actually don't know what is the construction ssh user#host -t '...' and how it works. If someone explains, I will be grateful) and I can type commands manually. But I don't know how to make server run my program using the script as I plan. Please help me to find solution.
I think you want to create a "detached" tmux session, then use tmux send-keys to send command instructions to the session. Also, I would use the full path to program. E.g. something like this
ssh user#host << EOF
tmux new-session -t session1 -d
tmux send-keys -t session1 "/path/to/the/program $argument" C-m
tmux kill-session -t session1
EOF
Tell tmux to run the program for you.
ssh user#host "tmux new-session -t session1 './program \"$argument\"'"
This runs program instead of a shell in the (only) window of the new session. When the program exits, the window is closed and along with it the session ends. (Quoting can get a bit hairy, so I'm calling that out-of-scope for this question.)
You can start a new tmux session in detached mode and then send commands with tmux send-keys command:
#! /bin/bash
argument='12345678'
ssh user#host << EOF
tmux new-session -t session1 -d
tmux send-keys -t session1:1 "./program $argument && tmux -d" ENTER
EOF
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 need to execute several bash scripts in background with a screen devoted to each script within byobu session.
So how to invoke a byobu window for each script like:
$byobu-multiple script1.bash script2.bash ...
Modern byobu is based on tmux, so you can simply use the tmux new-window command.
You can do one for each command:
tmux new-window script1.bash
tmux new-window script2.bash
If you really need a one-liner you could use xargs:
echo script1.bash script2.bash | xargs --max-args=1 tmux new-window
If you have multiple byobu sessions running in parallel, you can run tmux list-sessions to see them, and could append the tmux new-window command with -t to point it at a specific session.
How would you go about sending a command via SSH:
ssh user#server -t
Which will create a tmux session, send commands to it and then attach - allowing you to manually work on the interactivity presented from the commands? This has to be done while at the same time logging in as root - NOT via sudo.
ssh user#server -t "bash -c \"su - -c \"tmux -d \; apt-get update \; apt-get upgrade\" root\""
or simply without bash -c (which was an attempt at getting SSH to show the tmux window - as it (when using su - instead of sudo) would not display the shell. This was only when running it from a script - when run directly as a shell command there was no issue.
Another attempt have been:
ssh user#server -t "su - -c \"tmux new-session -n $session_name && tmux send-keys -t $session_name \"$command\"\" root"
This latter work relatively well - only it attaches to the tmux session and as such commands are sent after exiting the tmux manually only.
The idea is basically to automatically create a tmux session in which some commands are run - and then attach to it. This is to be done as a part of a bash script.
Any ideas?
For those interested, the command I found to work was:
ssh -p22 user#server -t "su - root -c \"tmux -d $session_name\;\"-t $session_name -c \"apt-get update\"\" \; \"-t $session_name -c \"apt-get upgrade\"\"\""
However it does not make you enter the tmux - merely sends back the input from the tmux to the user. A step in the right direction but not quite there..!
Very open to more efficient answers!
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