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

I want to write a shell script that does this:

  • First, create a tmux session
  • Second, run a ruby script called "run.rb" INSIDE the tmux session

In pseudo-code, what I want to do:

tmux new -s my_session
ruby run.rb     # NOTE: I want this to run inside the my_session tmux session.
tmux detach

How do I do this? (More posts I read, more confusing it gets.)


Solution 1:

#!/bin/bash
tmux 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.sh or chmod +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.

Solution 2:

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/bash
tmux new-session -d -s my_session 'ruby run.rb'

Solution 3:

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 htop
tmux split-window;                             # split the detached tmux session
tmux 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.

Solution 4:

You could use teamocil to do this easily. You could just create a YAML file:

windows:
  - name: rubysession
    root: ~
    layout: tiled
    panes:
      - ruby run.rb; tmux detach

If you named it 'rubysession.yml' then run:

teamocil rubysession

And that would work perfectly for your purpose and require no hacks. Also teamocil is awesome for loads of other uses!

Solution 5:

If you want to keep your tmux session alive after starting some commands, a possible solution is to start a bash with an init file:

tmux new -d -s mysession "bash --init-file foo.script"

where foo.script would contain your commands. Alternatively, you can feed the command to the shell directly from the command line:

tmux new -d -s mysession2 "bash --init-file <(echo ruby run.rb)"

Note that --init-file was meant for reading system wide initialization files like /etc/bash.bashrc so you might want to 'source' these in your script.