在屏幕会话的多个进程中自动粘贴命令 [英] Automatically paste commands in multiple processes of a screen session
问题描述
我在文本文件中包含几串 linux
命令,用 \ n \ n
分隔,我想自动将每个粘贴到给定的屏幕进程中.为了清楚起见,假设我的 command.txt
仅包含:
I have several bunch of linux
commands contained in a text file, separated by \n\n
, and I would like to automatically paste each in given screen processes. For the sake of clarity, let's say my command.txt
simply contains :
#first bunch of commands:
executable_script1.sh
mv executable_script1 directory1
#second bunch of commands:
executable_script2.sh
mv executable_script2 directory2
因此,第一组命令将运行 executable_script1.sh
,然后移动 executable_script1
.在此示例中,我的屏幕包含3个进程:
So the first bunch of commands would run executable_script1.sh
, after what it will move executable_script1
. In this example, my screen contains 3 processes:
0$ htop
1$ bash
2$ bash
进程的名称无关紧要,唯一重要的信息是我希望屏幕进程 N $
中的命令 N
为 0 $
始终是 htop
.
The name of the processes is irrelevant, the only important information is that I would like commands N
in screen process N$
, as 0$
is always a htop
.
到目前为止,我一直在相应的屏幕进程中手动复制/粘贴每条命令,这显然很有效,但是现在我将处理40多个命令和尽可能多的屏幕进程.那么,如何自动将命令 N
粘贴到 N $
屏幕终端?我认为 bash
/ shell
脚本可以解决这个问题,但是我对它的理解还不够流利.我目前使用 python2
脚本生成我的 command.txt
文件,因此请注意,我可以通过一堆命令轻松创建一个 txt
文件如果需要的话.
As for now, I have been copying/pasting manually each bunch of commands in the corresponding screen processes, which worked obviously, but now I will be dealing with more than 40 bunch of commands and as many screen processes. So, how could I paste the commands N
to the N$
screen terminal automatically? I think a bash
/shell
script could do the trick, but I am not fluent enough with it. I currently use a python2
script to generate my command.txt
file, so mind that I could create one txt
file by bunch of commands pretty easily if needed.
您能帮我吗?请随时询问任何缺少的信息.
Could you help me with this? Please feel free to ask for any missing information.
PS:我也曾在 Unix Stackexchange 上问过这个问题,但是这个论坛的人数似乎少得多...如果我们发现在这里找到答案,我也将邀请答题者将其粘贴到我的Unix Stackexchange问题下,因为这可能会帮助其他人!
PS: I also asked this question on Unix Stackexchange, but this forum seems far less populated... If we find an answer here, I will invite the answerer to paste it under my Unix Stackexchange question as well, as this could help others!
推荐答案
I finally found my answer, thanks to this post! Sometimes it just takes some other keywords to find a solution, so I will answer this question in case some other fellows end up here.
使用 bash
命令将命令自动粘贴到屏幕中:
Automatically paste commands in the screen with the bash
command :
screen -x screen_name -p 1 -X stuff 'executable_script1.sh\n'
其中 -p 1
是指 1 $
屏幕过程.请注意,命令末尾的 \ n
是必需的,就像在粘贴命令行后按Enter一样.
where -p 1
refers to the 1$
screen process. Note that \n
at the end of the command is necessary, as when you press enter after pasting a command line.
1)创建要在其中工作的 screen
会话(此处称为"screen_name"):
1) Create the screen
session you want to work in (here named 'screen_name') :
screen -S screen_name
具有用于所有命令的足够的进程(在我的示例中, 0 $ htop
加上2个进程: 1 $
和 2 $
).请注意,您可以在主目录中编辑 .screenrc
,以便默认情况下,屏幕会话以给定数量的进程开始.对于此示例,我的 .screenrc
包含:
with enough processes for all the commands (in my example, 0$ htop
plus 2 processes : 1$
and 2$
). Note that you can edit .screenrc
in your home directory so that screen sessions start with a given number of processes by default. For this example, my .screenrc
contains :
screen -t htop
screen -t
screen -t
2)为每条命令创建bash文件,以由不同的屏幕进程执行.
2) Create bash files for every bunch of commands, to be executed by the different screen processes.
这里我有2个文件,其中 screen1
包含:
Here I have 2 files, screen1
containing :
#!/bin/bash
screen -x screen_name -p 1 -X stuff 'executable_script1.sh\n'
screen -x screen_name -p 1 -X stuff 'mv executable_script1 directory1\n'
和 screen2
包含:
#!/bin/bash
screen -x screen_name -p 2 -X stuff 'executable_script2.sh\n'
screen -x screen_name -p 2 -X stuff 'mv executable_script2 directory2\n'
3)使用以下命令一次将所有命令粘贴到终端中:
3) Paste all your commands at once in a terminal, with :
bash /path_to_screen1/screen1 & /path_to_screen2/screen2 &
您可以立即关闭此终端,即使您进行了长时间的计算,因为它所做的只是将命令粘贴到 screen
中.手动打开您的 screen
会话以验证这些行是否正在执行.
You can close this terminal immediately, even if you have long run calculations, as all it does is paste the command into screen
. Manually open your screen
session to verify that these lines are being executed.
不用说,如果您有大量要传递给许多屏幕处理程序的命令,则可以创建bash文件并通过脚本(使用 python
粘贴命令(第2步和第3步)>例如).同样,如果需要, executable_script1.sh
可以包含 python
调用,与普通终端一样,可以使用 python python_script.py
.
Needless to say, if you have a great number of commands to pass to many screen processes, you can create the bash files and paste the commands (steps 2 and 3) via a script (with python
for instance). Also executable_script1.sh
can contain python
calls if needed, with python python_script.py
, as in a normal terminal.
希望这对其他人有帮助!
Hope this will help others!
这篇关于在屏幕会话的多个进程中自动粘贴命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!