如何编写启动 tmux 会话的 shell 脚本,然后运行 ruby 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31902929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write a shell script that starts tmux session, and then runs a ruby script
提问by hackstar15
I want to write a shell script that does this:
我想编写一个执行此操作的 shell 脚本:
- First, create a tmux session
- Second, run a ruby script called "run.rb" INSIDE the tmux session
- 首先,创建一个 tmux 会话
- 其次,在 tmux 会话中运行一个名为“run.rb”的 ruby 脚本
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.)
我该怎么做呢?(我读的帖子越多,就越混乱。)
回答by K M Rakibul Islam
#!/bin/bash
tmux new-session -d -s my_session 'ruby run.rb'
Create a file named
my_script.shand give it the above contents.Make the file executable by running:
chmod 755 my_script.shorchmod +x my_script.shThen run the shell script:
./my_script.sh
创建一个名为的文件
my_script.sh并为其提供上述内容。通过运行使文件可执行:
chmod 755 my_script.sh或者chmod +x my_script.sh然后运行shell脚本:
./my_script.sh
Making the shell script executable
使 shell 脚本可执行
When you perform the chmod 755 filenamecommand 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.
当您执行chmod 755 filename命令时,您允许每个人读取和执行文件,并且文件所有者也可以写入文件。对于应该通过网络服务器运行的 Perl 和其他脚本,您可能需要它。如果将 755 应用于目录,则意味着每个人都可以访问该目录并获取其文件列表。
These permissions are usually translated into textual representation of rwxr-xr-x.
这些权限通常被翻译成rwxr-xr-x.
You can alternatively use chmod +x file_nameon a file to make it executable.
您也可以chmod +x file_name在文件上使用以使其可执行。
回答by Nick
K M Rakibul Islam's updated code contains an unnecessary detach command at the end which causes an error message "no client found" (my_sessionhas already been detached and thus is not in scope so tmux cannot understand which session you want to detach). The correct code should be:
KM Rakibul Islam 的更新代码在末尾包含一个不必要的分离命令,这会导致错误消息“未找到客户端”(my_session已经分离,因此不在范围内,因此 tmux 无法理解您要分离哪个会话)。正确的代码应该是:
#!/bin/bash
tmux new-session -d -s my_session 'ruby run.rb'
回答by Quang Van
With some experimenting, I figured out how to control tmux via shell script.
通过一些实验,我想出了如何通过 shell 脚本控制 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'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 htopin both.
以上将 tmux 会话拆分为两个窗口,并htop在两个窗口中运行。
To answer original question, you can run a ruby script and not detached the tmux session with command below:
要回答原始问题,您可以运行 ruby 脚本而不是使用以下命令分离 tmux 会话:
tmux new-session -s ruby_session 'ruby run.rb'; # open tmux session and run ruby script.
回答by Tom Anthony
You could use teamocilto do this easily. You could just create a YAML file:
您可以使用teamocil轻松完成此操作。您可以创建一个 YAML 文件:
windows:
- name: rubysession
root: ~
layout: tiled
panes:
- ruby run.rb; tmux detach
If you named it 'rubysession.yml' then run:
如果您将其命名为“rubysession.yml”,则运行:
teamocil rubysession
And that would work perfectly for your purpose and require no hacks. Also teamocil is awesome for loads of other uses!
这将非常适合您的目的,并且不需要任何黑客攻击。此外,teamocil 也适用于许多其他用途!
回答by jeroent
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 会话处于活动状态,可能的解决方案是使用 init 文件启动 bash:
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:
其中 foo.script 将包含您的命令。或者,您可以直接从命令行将命令提供给 shell:
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.
请注意,--init-file 用于读取系统范围的初始化文件,例如 /etc/bash.bashrc,因此您可能希望在脚本中“获取”这些文件。
回答by doc.aicdev
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:
我不确定这对你来说是否仍然有趣,但我想给你一个答案/提示:例如,如果你想通过 shell 脚本启动多个 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
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".
为了演示,脚本将创建文件夹 test_1 和 test_2。之后,我定义了一个包含两个文件夹的数组并运行这两个文件夹并使用当前文件夹名称启动 tmux 会话并执行命令“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:
如果您想遍历当前目录中的所有子目录,请将“for i in "${directories[@]}”替换为“for f in *; 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
Here is a link to the gist file: https://gist.github.com/AICDEV/cf1497793bb1c27cb9b94d56c209ad6f
这是要点文件的链接:https: //gist.github.com/AICDEV/cf1497793bb1c27cb9b94d56c209ad6f

