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

后端 未结 6 674
暗喜
暗喜 2020-12-08 03:53

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
  • <
6条回答
  •  南方客
    南方客 (楼主)
    2020-12-08 04:33

    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

提交回复
热议问题