Shell脚本:打开多个终端并运行命令? [英] Shell script: open multiple terminals and run commands?

查看:277
本文介绍了Shell脚本:打开多个终端并运行命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个非常简单的shell脚本,该脚本可以打开并运行我的程序的五个实例.对于批处理,我会做这样的事情:

  @echo关闭python webstore.py 55530python webstore.py 55531python webstore.py 55532出口 

这将打开三个终端,并使用不同的命令行参数在每个终端上运行命令.如何使用在每个基于unix的平台上运行的shell脚本创建相同的脚本?我已经看到了一些用于打开终端的命令,但是它们是特定于平台的(gnome-termial,xterm等).

解决方案

我将如何使用在每个基于unix的平台上运行的shell脚本来创建相同的脚本?

您要的内容有点不合理.这样考虑:在Windows上,您始终处于其
(来源: github.io )

(这将在SSH,Linux控制台或以上任何其他情况下使用.)

但是,如果您不关心将要运行的命令的输出,则可以像这样将它们与当前终端会话分离:

  command1&/dev/null&command2&/dev/null&command3&/dev/null& 

请注意:

  1. 将并行运行命令.
  2. 不会显示任何输出.如果删除&>/dev/null ,则每个命令的输出将相互缠绕,这可能不是您想要的.
  3. 关闭终端通常会杀死其子进程,在这种情况下,它将杀死您的在后台工作的 command 实例.

上面提到的问题可以解决,但是我认为这个答案有点超出范围.

我个人会选择 tmux 还是分离解决方案,具体取决于我是否需要查看控制台输出.

I'm trying to create a very simple shell script that opens and runs five instances of my program. With batch, I would do something like this:

@echo off
python webstore.py 55530
python webstore.py 55531
python webstore.py 55532
exit

That would open three terminals and run the commands on each of them with different command line parameter. How would I create the same with a shell script that runs on every single unix-based platform? I've seen some commands for opening terminals, but they are platform specific (gnome-termial, xterm and so on).

解决方案

How would I create the same with a shell script that runs on every single unix-based platform?

What you're asking for is a bit unreasonable. Think about it that way: on Windows, you're always inside its Desktop Window Manager, period, and the only choice you have is between Powershell and cmd.exe. But on Linux, it's a little more complicated:

  1. Like you said, you can have either rxvt or xterm installed.
  2. This is not the only issue though - you can use any window manager. While this does not matter much here,
  3. You can be either using Xorg, or Wayland. Continuing on,
  4. You can not use any graphical environment at all, e.g. run everything in Linux console! Which, unless you use fancy programs such as fbterm or tmux, is pretty much incapable of multitasking, yet alone spawning new windows.
  5. That being said, you may even not use this computer physically at all, because you're connecting to it from SSH. No remote windows here either (unless you use stuff as X11 forwarding).
  6. Finally, you can use zsh, bash, sh, fish etc. that all come with their own idiosyncrasies.

IMO your best bet is to test in your script which programs are installed, or script around a terminal multiplexer such as tmux and require it to be installed on the target machine. tmux in action:


(source: github.io)

(This will work in either SSH, Linux console, or any other scenario above.)

If you, however, do not care about the output of the commands you're about to run, you can just detach them from your current terminal session like this:

command1 &>/dev/null &
command2 &>/dev/null &
command3 &>/dev/null &

Be mindful that this:

  1. Will run the commands in parallel.
  2. Won't show you any output. If you remove &>/dev/null, the output from each command will interwine with each other, which is probably not what you want.
  3. Closing the terminal usually kills its children processes, which, in this case, will kill your command instances that work in the background.

Issues mentioned above can be worked around, but I believe it is a little out of scope for this answer.

Personally I'd go either for tmux or for detaching solution depending on whether I need to see the console output.

这篇关于Shell脚本:打开多个终端并运行命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆