Open In App

How to write a shell script that starts tmux session, and then runs a ruby script

Last Updated : 07 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

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.

Features

  • Managing Windows: Can create multiple windows within the tmux session, each of the sessions can hold more than one pane.
  • Session Management: One can create and manage multiple Tmux sessions. Each session can have multiple windows and panes.
  • Pane Splitting: The terminal session can be split into different panes, which can be split both horizontally and vertically.
  • Customization: Tmux sessions are highly customizable and allow to configuration of a lot of options.
  • Detaching and Reattaching: The Tmux session can be detached without closing it, that is it will be running in the background and thus can be reattached.

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.

Steps to create and execute the bash script

Step 1: Open the terminal window.

Step 2: Use any text editor like Vim, Vi, Nano, or Gedit.

$ gedit tmux_script.sh

Step 3: Now use the following code in the script.

  • tmux_script.sh
#creates new session
tmux new-session -d -s my_ruby_session
# Creates the session for the running ruby script
tmux send-keys -t my_ruby_session "ruby ruby_script.rb" C-m
# Attaches to session
tmux attach-session -t my_ruby_session

Step 4: Make the script executable using the following command.

$ chmod +x tmux_session.sh

Step 5: Execute the script.

$ ./tmux_session.sh

Output:

Execute the script which we have previously created, by making the script executable.

Granting Execution Permissions

Executing Script

Output

Here once we run the script, a new tmux session opens up. The below green strip indicates that we are currently in a tmux session. This automatically runs the ruby script that we created before. The Tmux session status bar informs you that you are currently within a Tmux session, which allows for enhanced control over the terminal. Additionally, as part of the Tmux session’s setup, it automatically executes the Ruby script that we created previously. This means that as soon as the Tmux session is created, your Ruby script starts running within it without any manual intervention.

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.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads