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.

Similar Reads

Introduction to Tmux

Tmux is an open-source terminal multiplexer for Unix-like operating systems. Tmux can be used to manage multiple terminal sessions within a single terminal window or emulator. With Tmux, you can create, organize, and switch between different terminal sessions, each with its own set of windows and panes. With the help of Tmux, we can detach processes from their terminals. Multiple terminal sessions can be created within a single terminal session. Tmux provides a way to create and switch between multiple terminal sessions. Tmux can be extremely helpful for users who want to work with multiple terminal sessions....

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....

Steps to create and execute the bash script

Step 1: Open the terminal window....

Conclusion

In this article, we learned how to create a shell script that initializes a tmux session and fires up a ruby script while inside the tmux session. Here will be able to automatically commence a Tmux session and perform certain tasks. We also saw how to use a shell script which will automatically start a tmux session and then run a ruby script....

Contact Us