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

后端 未结 6 658
暗喜
暗喜 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:25

    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'
    
    0 讨论(0)
  • 2020-12-08 04:30

    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.
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-08 04:34

    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.

    0 讨论(0)
  • 2020-12-08 04:37

    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!

    0 讨论(0)
  • 2020-12-08 04:38
    #!/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.

    0 讨论(0)
提交回复
热议问题