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
  • 15,400
  • 13
  • 40
  • 51
  • cd will work in scripts, it just won't work well in a script in a script on its own. Where is this code going to be run from (gnometerminal, launcher, AltF2 etc)? – Wilf Oct 16 '15 at 22:58
  • @Wilf - yes, Phoenix is a terminal profile I've defined. I can run it from wherever someone tells me is best! Currently the file is in `~/Desktop` just to make it easy to find. – EmmyS Oct 16 '15 at 23:00
  • @Wilf - you edited your comment while I was answering. I've been running the file from another terminal window up to this point, although I'd like to have it on a launcher to run on a click for convenience. – EmmyS Oct 16 '15 at 23:01
  • If you can live without the tabs being first class Gnome Terminal tabs then check out `tmux`. This would also give you the benefit of not losing context if X-Windows crashes on you. – chicks Oct 17 '15 at 01:40
  • @chicks - I'm not sure what you mean by "first class Gnome Terminal tabs". – EmmyS Oct 18 '15 at 16:02
  • `tmux` has tabs within it, but they wouldn't look or work like Gnome Terminal's tabs. https://tmux.github.io/ss-tmux1.png shows how the tabs look in tmux. – chicks Oct 18 '15 at 16:15
  • @chicks - can `tmux` be scripted? The whole point of this is to not have to open each window/tab/whatever individually. – EmmyS Oct 27 '15 at 15:32
  • @EmmyS: absolutely, `tmux` is much easier to automate than Gnome Terminal or `screen` IMHO. [my start_tmux](https://github.com/chicks-net/chicks-home/blob/master/bin/start_tmux) script shows an example of how to do this. Check out [this tutorial](https://smartosadmin.wordpress.com/2015/03/15/automating-development-workflow-with-tmux/) to get started. – chicks Oct 27 '15 at 16:44

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
  • 122
  • 1
  • 3