2

I want to use tmux's send-keys to run a series of commands when I log into a remote server

In the linked answer, a single command is used. I've found that I can put a series of them but I've never gotten more than 2 of them to successfully run. Is it possible to introduce a pause between them (since I assume the problem is one of tmux executing them before the remote host can respond)?

The commands will span a change of the logged-in user, which hasn't been problem so far, but that's one reason I can't simply have a script on the server that I run. (tmux doesn't care if I change user but a script can't do that and then continue running.)

So how can I go beyond just 2 commands?

I doubt it is relevant, but I'm running zsh locally and bash on the server.

3
  • 2
    Are you running commands like tmux send-keys -t s1 username from a shell script or are you binding keys? If it is a script then just use sleep in the script. The correct tool to do this is probably expect which can react to the output rather than just typing blindly.
    – icarus
    Oct 5, 2021 at 23:07
  • I'm actually running functions that exist on the server.
    – iconoclast
    Oct 5, 2021 at 23:52
  • Sorry, I don't understand. This is no doubt blindingly obvious to you but we can't see what you are doing. How are you running your two commands? The linked answer gives 2 solutions. The sendkeysone looks like a script to me. Are you actually typing tmux new -dmy_session \; send mycommand Enter or do you have this in a function or in a file?
    – icarus
    Oct 6, 2021 at 5:44

2 Answers 2

1

I don't fully understand the question either, but expect may help you. It can send input to interactive sessions and parse responses, avoiding things like manual delays to wait for logins, commands to finish, etc.

1
  • Is it possible to write an expect script that (1) issues an SSH command, (2) selects a number at a DUO-related prompt, (3) waits for me to approve the connection in the DUO iPhone app, (4) changes user once logged into the remote host, and (5) leaves the shell open for interactive use after the expect script terminates? I'm guessing that either step #3 or step #5 might be a problem.
    – iconoclast
    Apr 17, 2023 at 21:09
1

Just do a 'sleep' between the tmux commands....

tmux send-keys -t 1 "echo -n hello " Enter
sleep 5
tmux send-keys -t 1 "echo world" Enter

However the usual reason to wait is becuase something needs to happen before continuing - relying on a fixed interval is not good engineering. It would be much safer to use a proper expect script as @FazJaxton suggests.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .