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

73,044

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

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.

Solution 5

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!

Share:
73,044
hackstar15
Author by

hackstar15

Updated on July 05, 2022

Comments

  • hackstar15
    hackstar15 almost 2 years

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

  • fsanches
    fsanches almost 9 years
    Alternatively he can change the file permission with "chmod +x my_script.sh", with "+x" meaning "give(+) eXecutable rights".
  • K M Rakibul Islam
    K M Rakibul Islam almost 9 years
    Yes, for sure! He can change the file's permission that way too.
  • chepner
    chepner almost 9 years
    When the new session is created, the bash script is on hold, because the first tmux command doesn't exit while you are still attached to the new session.
  • hackstar15
    hackstar15 almost 9 years
    @KMRakibulIslam sorry, i need to run.rb to run INSIDE the tmux session. your solution runs it in the original terminal. Do you know how to run the run.rb script inside the tmux?
  • K M Rakibul Islam
    K M Rakibul Islam almost 9 years
    @hackstar15 Try: tmux new-session -d -s my_session 'ruby run.rb' tmux detach
  • hackstar15
    hackstar15 almost 9 years
    @KMRakibulIslam Perfect!
  • Swarup Donepudi
    Swarup Donepudi over 8 years
    This is not working for me. Gives me session not found error: $cat tmux_sh.sh #!/bin/bash echo "step 1" tmux new-session -d -s rtb123 'vagrant up' echo "step 2" tmux detach -s rtb123 ./tmux_sh.sh step 1 step 2 session not found: rtb123
  • sargas
    sargas over 7 years
    tmux new-session -d ... The -d part creates a new session already detached. Isn't the line tmux detach -s my_session redundant then?
  • Nick stands with Ukraine
    Nick stands with Ukraine over 7 years
    It's not working for me either. I am getting "no current client".
  • B. Shea
    B. Shea over 5 years
    Fix your answer. Meaning use your "update". The top portion does not answer the question fully - he says: "INSIDE the tmux session". (Added an edit myself to fix it)
  • Admin
    Admin over 4 years
    How do you execute multiple commands instead of just one like 'ruby run.rb'
  • FullStack Alex
    FullStack Alex over 4 years
    @Muddz you could for example run first command && second command && and so on or you could put your commands in a shell script and run it instead of the commands: sh /path/to/script.sh
  • Franklin Yu
    Franklin Yu over 2 years
    To be more precise, this is actually splitting a single window into two panes (stacked by default).