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

I want to write a shell script that does this:

  • First create a tmux session
  • Secondly, run a ruby ​​script called "run.rb" INSIDE in a tmux session

In the 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 can I do it? (The more posts I read, the more confusing it gets.)

+37
source share
6 answers
#!/bin/bash
tmux new-session -d -s my_session 'ruby run.rb'
  1. Create a file with the name my_script.shand pass the above contents to it.

  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

Creating an executable script

chmod 755 filename , . Perl , -. 755 , , .

rwxr-xr-x.

chmod +x file_name , .

+37

K M Rakibul Islam , "no client found" (my_session , , , tmux , ). :

#!/bin/bash
tmux new-session -d -s my_session 'ruby run.rb'
+21

teamocil, . YAML:

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

"rubysession.yml", :

teamocil rubysession

. , teamocil !

+3

tmux , - bash :

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

foo.script . , :

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

, --init-file , /etc/bash.bashrc, "" .

0

, , /: , , tmux - , :

# 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

test_1 test_2. , tmux "ls -la".

, "for i " $ {directoryies [@]} "" f *; do ". , :

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

gist: https://gist.github.com/AICDEV/cf1497793bb1c27cb9b94d56c209ad6f

0

, , tmux .

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 a '--target' option to target specific pane.
tmux a;                                        # open (attach) tmux session.

tmux htop .

, ruby tmux :

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

Source: https://habr.com/ru/post/1613746/


All Articles