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

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


#!/bin/bashtmux new-session -d -s my_session 'ruby run.rb'
  1. Create a file named my_script.sh and give it the above contents.

  2. Make the file executable by running:

    chmod 755 my_script.shorchmod +x my_script.sh

  3. Then run the shell script:

    ./my_script.sh

Making the shell script executable

When you perform the chmod 755 filename command you allow everyone to read and execute the file, and the file owner is allowed to write to the file as well. You may need this for Perl and other scripts that should be run via a webserver. If you apply 755 to a directory, it means that everyone can go to it and get its file listing.

These permissions are usually translated into textual representation of rwxr-xr-x.

You can alternatively use chmod +x file_name on a file to make it executable.


K M Rakibul Islam's updated code contains an unnecessary detach command at the end which causes an error message "no client found" (my_session has already been detached and thus is not in scope so tmux cannot understand which session you want to detach). The correct code should be:

#!/bin/bashtmux new-session -d -s my_session 'ruby run.rb'


With some experimenting, I figured out how to control tmux via shell script.

tmux new-session -d -s htop-session 'htop';  # start new detached tmux session, run htoptmux split-window;                             # split the detached tmux sessiontmux send 'htop -t' ENTER;                     # send 2nd command 'htop -t' to 2nd pane. I believe there's a `--target` option to target specific pane.tmux a;                                        # open (attach) tmux session.

The above splits the tmux session into two window, and runs htop in both.

To answer original question, you can run a ruby script and not detached the tmux session with command below:

tmux new-session -s ruby_session 'ruby run.rb';  # open tmux session and run ruby script.