Creating the Script
Step 1: Create a simple ruby script or you can use any ruby script of your choice that you want to run within the tmux session.
- ruby_script.rb
greetings = "Hello Geeks"
puts greetings
Step 2: Use the following line to create a new Tmux session.
#creates new session
tmux new-session -d -s my_ruby_session
- tmux new-session: This command is used for creating a new Tmux session.
- -d: This option tells Tmux to create the new session in detached mode.
- -s my_ruby_session: Specifies the name of the new Tmux session, in this case, “my_ruby_session.”
Step 3: Configure the tmux multiplexer to send keyboard strokes to the tmux sessions.
# Creates the session for the running ruby script
tmux send-keys -t my_ruby_session "ruby ruby_script.rb" C-m
- tmux send-keys: This is the Tmux command for sending keystrokes to a Tmux session.
- -t my_ruby_session: This specifies the target session or window where you want to send the keystrokes. Here we are targeting the “my_ruby_session”.
- ruby ruby_script.rb: This is the actual string of keystrokes that you want to send. In this case, it’s the command ruby ruby_script.rb, which appears to be a Ruby script named “ruby_script.rb” that you want to execute within the Tmux session.
- C-m: This represents a control character, specifically “Control-M,” which is equivalent to the Enter key.
Step 4: The tmux attach-session command is used in the Tmux terminal multiplexer to attach to an existing Tmux session.
# Attaches to session
tmux attach-session -t my_ruby_session
- tmux attach-session: This is the base command for attaching to an existing Tmux session.
- -t my_ruby_session: This option specifies the target session you want to attach to.
How to write a shell script that starts tmux session, and then runs a ruby script
Scripts are important for streamlining workflows and automating tasks. One such way to streamline workflow is by using Tmux [Terminal Multiplexer]. When used along with shell scripts we will be able to automatically commence a Tmux session and perform certain tasks. In this article, we shall see how to use a shell script that will automatically start a tmux session and then run a ruby script.
Contact Us