98

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

Blaszard
  • 30,954
  • 51
  • 153
  • 233
hackstar15
  • 1,259
  • 1
  • 10
  • 14

6 Answers6

99
#!/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.

B. Shea
  • 829
  • 14
  • 29
K M Rakibul Islam
  • 33,760
  • 12
  • 89
  • 110
  • Alternatively he can change the file permission with "chmod +x my_script.sh", with "+x" meaning "give(+) eXecutable rights". – fsanches Aug 09 '15 at 10:34
  • Yes, for sure! He can change the file's permission that way too. – K M Rakibul Islam Aug 09 '15 at 10:36
  • 2
    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. – chepner Aug 09 '15 at 18:12
  • 1
    @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? – hackstar15 Aug 09 '15 at 21:13
  • @hackstar15 Try: tmux new-session -d -s my_session 'ruby run.rb' tmux detach – K M Rakibul Islam Aug 09 '15 at 21:22
  • 1
    @KMRakibulIslam Perfect! – hackstar15 Aug 09 '15 at 21:27
  • 3
    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 – Swarup Donepudi Oct 29 '15 at 22:56
  • `tmux new-session -d ...` The -d part creates a new session already detached. Isn't the line `tmux detach -s my_session` redundant then? – sargas Aug 29 '16 at 23:31
  • It's not working for me either. I am getting "no current client". – Nick Sep 14 '16 at 09:54
  • 1
    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) – B. Shea Dec 18 '18 at 16:31
32

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'
Nick
  • 2,924
  • 4
  • 36
  • 43
  • How do you execute multiple commands instead of just one like `'ruby run.rb'` –  Jan 15 '20 at 14:05
  • 2
    @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` – FullStack Alex Jan 17 '20 at 09:12
27

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.
Quang Van
  • 11,475
  • 12
  • 57
  • 61
6

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.

jeroent
  • 2,008
  • 2
  • 15
  • 8
3

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!

Tom Anthony
  • 791
  • 7
  • 14
2

I am not sure if this is still interesting for you, but I like to give you an answer / hint: in case you want, for example, start multiple tmux sessions by shell script and execute some command, you can do it like follow:

# just for test and show case
mkdir test_1 test_2

echo "current tmux sessions"
tmux ls

echo "kill all tmux sessions"
tmux kill-server

declare -a directories=("test_1" "test_2")

for i in "${directories[@]}"
do
cd ${i}
pwd
tmux new -d -s ${i} "ls -la"
cd ..
done

For the demonstration, the script will create a folder test_1 and test_2. After that I have defined an array with the two folders and run through the two folders and start a tmux session with the current folder name and execute the command "ls -la".

If you like to run through all sub directories in your current directory, please replace "for i in "${directories[@]}" with "for f in *; do". Here is an example that also exclude symbolic folders:

echo "current tmux sessions"
tmux ls

echo "kill all tmux sessions"
tmux kill-server dependencies

     for f in *; do
        if [[ -d "$f" && ! -L "$f" ]]; then
            cd ${f}
            pwd
            tmux new -d -s ${i} "ls -la"
            cd ..
        fi
    done

Here is a link to the gist file: https://gist.github.com/AICDEV/cf1497793bb1c27cb9b94d56c209ad6f

doc.aicdev
  • 96
  • 8