1

I've never written a shell script before, but I have a web app that needs a whole bunch of things running at once, and it's irritating to have to open each tab, change directories, start my processes, etc.

I'm basing my file on the one shown in this question.

The first chunk (the redis call, which does the cd on its own) works, sort of - it opens a single terminal tab and runs redis. (I only separated it out for testing purposes; ideally it would be part of the array and all tabs would open in a single terminal window.)

The second chunk is where I'm running into problems. Apparently cd doesn't work in shell scripts, but that particular link is way over my head, and I don't understand how to implement any of the solutions shown there. The specific error I get is:

bash: cd ~/phoenix/RenderService; npm start: No such file or directory

The tabs that open via the loop have prompts in the redis-stable directory:

enter image description here

Here's what I want to happen when I run the script:

  1. Open a terminal window with three tabs, using the Phoenix profile.
  2. The first tab should change to the redis-stable directory, then run src/redis-server
  3. The second tab should change to the phoenix/PhoenixServices directory, then run npm start
  4. The third tab should change to the phoenix/RenderService directory, then run npm start

The terminal window and all tabs should remain open after the commands have run.

Here's my existing code:

#!/bin/bash

# run redis, PhoenixServices, and RenderService

cd ~/redis-stable
gnome-terminal --tab-with-profile=Phoenix --title=Redis -e 'bash -c "src/redis-server; exec bash"'

tab=" --tab-with-profile=Phoenix"
options=(--tab --title=Terminal)

cmds[1]="'cd ~/phoenix/PhoenixServices; npm start'"
titles[1]="PhoenixServices"

cmds[2]="'cd ~/phoenix/RenderService; npm start'"
titles[2]="RenderService"


for i in 1 2; do
  options+=($tab --title="${titles[i]}"  -e "bash -c \"${cmds[i]} ; exec bash\"" )          
done

gnome-terminal "${options[@]}"


exit 0

Can anyone help a poor newb out?

EmmyS
  • 17,031

1 Answers1

1

If you don't need to actively monitor all 3 tabs, you can feed them into log files and open them when you need an update on activity. This is about how I would kick it off:

#!/bin/bash

cd ~/redis-stable; src/redis-server &> path/to/redis.log &
cd ~/phoenix/PhoenixServices; npm start &> path/to/phoenix.log &
cd ~/phoenix/RenderService; npm start &> path/to/render.log

You can see the answer to this stackoverflow question for more information on running parallel processes.

You can also use monitoring services to splash the terminal if certain events occur during the processes with until or watch sequences. See the answers to this superuser question for more ideas.

csworenx
  • 140