通过 SSH 和 运行 命令附加一个 tmux 会话

Attach a tmux session over SSH and run a command

这适用于附加具有指定名称的会话,如果不存在则创建:

tmux new-session -A -s encode

但我需要向 运行 添加命令,即

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

您需要查看 tmux send-keys 命令。来自 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.

在你的情况下你可以这样做

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

C-m 是回车键。 -d 标志用于创建会话,但不附加到它。