This question has a solution on ServerFault:
It uses the send-keys
command, which is documented in the man-pages:
The following is a "Hello World" example illustrating the use of the send-keys
command.
Step 1. Create a detached session:
user@host:~$ tmux new -d -s mySession
Step 2: Execute a command in the detached session:
user@host:$ tmux send-keys -t mySession.0 "echo 'Hello World'" ENTER
Step 3: Attach to the session:
user@host:$ tmux a -t mySession
You should see the following text displayed in the Tmux terminal window:
user@host:$ echo 'Hello World'
Hello World
user@host:$
This demonstrates that the echo
command was successfully sent and executed inside of the tmux session.
An adaptation of this technique to your commands might look something like the following:
user@host:~$ tmux send-keys -t mySession.0 exit ENTER
user@host:~$ tmux send-keys -t mySession.0 "cd .." ENTER
user@host:~$ tmux send-keys -t mySession.0 "cd app" ENTER
user@host:~$ tmux send-keys -t mySession.0 "rails console" ENTER