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

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 06:14:50
K M Rakibul Islam
#!/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.

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'

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!

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.

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!