简体   繁体   English

如何同时在多个选项卡/ shell中运行相同的linux命令?

[英]How do I run the same linux command in more than one tab/shell simultaneously?

Is there any tool/command in Linux that I can use to run a command in more than one tab simultaneously? Linux中是否有任何工具/命令可用于同时在多个选项卡中运行命令? I want to run the same command: ./myprog argument1 argument2 simultaneously in more than one shells (I want to increase this so as to put my code under stress later on) to check if the mutexes are working fine in a threaded program. 我想在多个shell中同时运行相同的命令: ./ myprog argument1 argument2 (我想增加它以便稍后将我的代码置于压力之下)来检查互斥锁是否在线程程序中正常工作。

I am kind of looking for something like what wall does. 我有点像墙壁那样的东西。 I can think of using tty's, but that just seems like a lot of pain if I have to scale this to many more shells. 我可以想到使用tty,但是如果我必须将它扩展到更多的shell,那就好像很痛苦。

Why not do something like 为什么不做类似的事情

for i in {1..100}
do
    ./myprog argument1 argument2 &
done

This is in case the shell is bash. 这是shell的bash。 You can look into other looping constructs in case of other shells. 在其他shell的情况下,您可以查看其他循环结构。

Running the jobs asynchronously with redirected output is the simplest solution, but if you really want each process to run in its own terminal, a good option is to use a terminal emulator like screen or tmux . 使用重定向输出异步运行作业是最简单的解决方案,但如果您确实希望每个进程都在自己的终端中运行,那么一个很好的选择是使用终端模拟器,如screentmux For example: 例如:

yes | sed 5q | while read k; do  # iterate 5 times
  tmux new-session -d 'sh -c "/path/to/myprog arg1 arg2; sh"'&
done

The trailing sh causes the session to remain alive after myprog terminates, and the intial yes pipeline is used instead of seq since it works where seq is not available (there are lots of ways to iterate!) 尾随sh会导致会话在myprog终止后保持活动状态,并且使用初始yes管道而不是seq因为它在seq不可用的情况下工作(有很多方法可以迭代!)

Once the sessions are running, you can attach to them individually to view the output. 会话运行后,您可以单独附加它们以查看输出。 (See tmux documentation for details.) (有关详细信息,请参阅tmux文档。)

Install the moreutulis package (with non-GNU parallel) or, preferably, GNU parallel (often packaged separately; SUSE calls it gnu_parallel, Ubuntu and RedHat call it parallel) so you can use parallel. 安装moreutulis软件包(非GNU并行),或者最好是GNU并行(通常单独打包; SUSE称之为gnu_parallel,Ubuntu和RedHat并行调用),这样你就可以使用parallel。 Then figure out how to specify the terminal your command runs in. For example 然后弄清楚如何指定命令运行的终端。例如

parallel yourscript -- tty1 tty2 pts6

Then you just have to figure out how to write "yourscript" to take the tty to run on as an argument, which is a separate question. 然后你只需要弄清楚如何编写“yourscript”来将tty作为一个参数运行,这是一个单独的问题。 :) :)

if you are using gnome terminal, try this one 如果你正在使用gnome终端,试试这个

gnome-terminal --maximize --tab-with-profile=default -e '/bin/sh -c ./myprog argument1 argument2' --tab-with-profile=default -e '/bin/sh -c ./myprog argument1 argument2' --tab-with-profile=default -e '/bin/sh -c ./myprog argument1 argument2'

please check gnome-terminal man page for more options 请查看gnome-terminal 手册页以获取更多选项

Use GNU Parallel's --tmux --fg : 使用GNU Parallel的--tmux --fg

parallel -j20 --tmux --fg echo doit {}\; sleep {} ::: {1..100}

Use ctrl-b n and ctrl-b p to cycle between the running terminals. 使用ctrl-b nctrl-b p在运行终端之间循环。

That way you can keep everything in a single window, but without the output from the jobs mixing. 这样你可以将所有内容保存在一个窗口中,但不会混合作业的输出。

I had the same situation when I was at college and I did something like this 我在大学的时候遇到了同样的情况,我做了类似的事情

for i in $(seq 1 10)
do
  xterm -e "./myprog argument1 argument2"&
done

This spawns 10 xterms each running your program in the background. 这会产生10个xterms,每个都在后台运行你的程序。

From comments it seems you don't need stdin, do need stdout, don't want ptys, don't want screen or tmux . 从评论看来你似乎不需要stdin,需要stdout,不需要ptys,不想要screentmux

Easy-peasy: 十分简单:

for seq in `seq 5`; do
        ./myprog argument1 argument2 >output$seq &
done

也许你可以(ab)使用clusterssh吗?

打开一堆终端,设置好所有内容,然后在每个终端中快速点击ENTER。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何在Linux中对Shell进行多项工作 - How can I Shell have more than one job in Linux 如何在 Linux 上启动时运行 shell 命令? - How do I run a shell command on startup on Linux? 我如何在linux的exec命令中执行多个命令 - How can I execute more than one command in exec command of linux expect 如何在一个终端上运行多个命令? - how to run more than one command one terminal? Nodejs Shell脚本在Linux上运行良好,但在Windows中运行不正常。 为什么不执行多个命令 - Nodejs shell script works fine in linux but not in Windows. Why won't it execute more than one command Linux中用于系统调用的多个命令 - more than one command for system call in linux 如果命令超过 1 行,如何在 shell 或 bash 中输入新行/移动到下一行而不执行命令? - How do I enter a new line/move to the next line without executing command while in a shell or bash if the command has more than 1 line? 使用execlp在Linux中使用多个参数作为字符串运行linux命令 - run linux command using execlp with more than one argument as string in c 如何在python中执行复杂的“find”linux shell命令 - How do I execute a complex “find” linux shell command in python 如何每3小时在shell中运行同一命令 - How can I run same command in a shell every 3h
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM