0

I have a csv file which includes a bunch of commands in the command line. I can just bash this csv file, but the commands will be executed sequentially in the same terminal. Is there a way to execute each one of these commands in a separate terminal, so that they are executed in parallel?

Example:

It's like:

python args1
python args2
...
python argsn
thanasissdr
  • 807
  • 1
  • 10
  • 26
  • You can put each command in the background? There's also this https://stackoverflow.com/questions/4404242/programmatically-launch-terminal-app-with-a-specified-command-and-custom-colors – pvg Oct 16 '17 at 16:27
  • Here is another question to check: https://stackoverflow.com/questions/989349/running-a-command-in-a-new-mac-os-x-terminal-window – Michael Sacket Oct 16 '17 at 16:28
  • What exactly does your CSV file look like? In general, you should not be able to simply execute it, as the field delimiters shouldn't be considered part of the arguments. – chepner Oct 16 '17 at 16:50
  • 1
    How is that a CSV file? – chepner Oct 16 '17 at 18:05
  • Huh? Why would you have bash commands mixed in with a `csv`? – Mark Setchell Oct 16 '17 at 18:05
  • @chepner 1st row: command1 2nd row command2 and so on. There's only one cell per row. – thanasissdr Oct 16 '17 at 18:10
  • Please show a proper example of your file else you are wasting everyone's time. – Mark Setchell Oct 16 '17 at 18:12
  • Why do you want separate Terminal windows for each? Wouldn't it be better to prefix each command's output with a tag - all in one window? – Mark Setchell Oct 16 '17 at 18:14
  • @thanasissdr You just have a regular text file. It might have been exported from something (like a spreadsheet, e.g.), but that isn't really relevant. – chepner Oct 16 '17 at 18:14
  • @chepner Excuse my ignorance, but I am rather new in this field. – thanasissdr Oct 16 '17 at 18:15

3 Answers3

2

You basically just have a shell script already. The only difference is that instead of running it as one script, you want to run each line as a separate command. How you open a new terminal is somewhat OS-dependent, but let's assume you would just use the xterm command. The following will treat your script commands.csv (or whatever it is named) as a data file.

while IFS= read -r cmdline; do
    xterm -e sh -c "$cmdline" &
done < commands.csv
chepner
  • 497,756
  • 71
  • 530
  • 681
2

If you want to do any serious software development on a Mac, I would suggest you install homebrew because Apple doesn't ship a package manager, but does ship ancient versions of all the tools most people use... Python, sed, PHP, Perl, awk, find, grep, make, git...

Once you have homebrew, I would recommend GNU Parallel which you can install with:

brew install parallel

Once you have GNU Parallel, you can run your commands in parallel with:

parallel --dry-run -a commands.csv

Or, maybe you would like the lines tagged with their names:

parallel --tag -a commands.csv

If you would like 8 to run at a time, add -j 8. If you want an Estimated Time of Arrival (when they should complete) add --eta and so on.


By the way, you can look for other tools, like Intel TBB, with:

brew search tbb

or pango, with:

brew search pango
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
-1

You can run commands in the background like

run-command &
run-some-other-command &
etc &

is that something you can use?

CodeR70
  • 447
  • 3
  • 2