Reputation: 69
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).
Upvotes: 3
Views: 5485
Reputation: 1
#! /usr/bin/bash // your bash
npx kill-port 3000 3001 3002 3003 // kill port if already run
current_directory=$(pwd) //your current directory
directories=("project-one" "project-two" "project-three" "project-four") for ((i=0; i<${#directories[@]}; i++)); do dir="${directories[i]}" full_path="$current_directory/$dir" echo "Starting server in $dir"
gnome-terminal --tab --title="$dir" --working-directory="$full_path" --command="bash -c 'nodemon app; exec bash'" gsettings --tab-position="bottom" done enter link description here
Upvotes: -3
Reputation: 14811
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:
rxvt
or xterm
installed.fbterm
or tmux
, is pretty much incapable of multitasking, yet alone spawning new windows.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:
&>/dev/null
, the output from each command will interwine with each other, which is probably not what you want.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.
Upvotes: 3