Drewsdesign
Drewsdesign

Reputation: 61

Attach a tmux session over SSH and run a command

This works to attach a session with a specified name or create if it doesn't exist:

tmux new-session -A -s encode

but I need to add a command to run ie.

tmux new-session -A -s encode 'ls /home/user/'

Upvotes: 1

Views: 3937

Answers (1)

tonchis
tonchis

Reputation: 648

You'll want to take a look at the tmux send-keys command. From the man

Send a key or keys to a window. Each argument key is the name of the key (such as C-a or npage ) to send; if the string is not recognised as a key, it is sent as a series of characters. The -l flag disables key name lookup and sends the keys liter- ally. All arguments are sent sequentially from first to last. The -R flag causes the terminal state to be reset.

In your case you can do

tmux new-session -d -A -s encode
tmux send-keys -t encode 'ls /home/users' C-m
tmux attach -t encode

C-m is the Enter key. The -d flag is to create the session, but not attach to it.

Upvotes: 5

Related Questions