简体   繁体   中英

Shell script: open multiple terminals and run commands?

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, eg 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:

tmux 在行动
(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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM