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.
CodePudding user response:
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
CodePudding user response:
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.)
CodePudding user response:
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