0

I have 5 python servers in different Directories. I want to write a bash/python script that can open 5 terminal tabs to run each server parallelly. As I also want to monitor the output of the server on the terminal I don't want them to run in the background.

#!/bin/bash
cd /home/user/dir_1
source venv/bin/activate
python manage.py runserver 8009
gnome-terminal --tab
cd /home/user/dir_2
source venv/bin/activate
python manage.py runserver 8001

This was my initial thought. But as these are servers, until one command is completed it will not go to another one so it cant achieve the desired result. As i don't have much experience with this kinda stuff please explain in a noobs langauage.

Toto
  • 89,455
  • 62
  • 89
  • 125
  • 1
    Did you try to add an ampersand `&` after the line in which you start the server. It will push the command in the background but its output should still be going to stdout meaning it should still be visible in the console. – schilli May 17 '21 at 18:36
  • Not your question, but maybe it's interesting anyway: In order to avoid sourcing the activate script everytime, you could just do `venv/bin/python manage.py runserver 8009`. It will still run the virtualenv python, but you save yourself the sourcing. Also, I'm not sure if it is critical that you source from another sendbox. – schilli May 17 '21 at 18:40
  • Yeah I tried that. But after it opens the 2nd terminal tab. it does not execute any commands. its not going in the 2nd directory. its still in the same directory (dir_1) in the 2nd tab – Tushar Solanki May 17 '21 at 18:41
  • Thanks for the other advice I didn't know we can do that. – Tushar Solanki May 17 '21 at 18:44
  • Alright. I see... Unfortunately, I'm not familiar with `gnome-terminal --tab`, so I can't tell for sure how to achieve what you want. – schilli May 17 '21 at 18:46
  • This looks like a promising answer: https://stackoverflow.com/a/54025197/6180150 – schilli May 17 '21 at 18:49
  • I hope you are not interested in some kind of gnome automation to show you logs in 5 tabs of terminal. – Saurav Kumar May 17 '21 at 19:24
  • i think you could put every server in separted script and run `gnome-terminal --tab script1` and `gnome-terminal --tab script2`, etc. I didn't test it but I think you can't open `gnome-terminal` in one line and in next line run command in this terminal. It doesn't work this way - all commands will be executed only in console in which you started script. You may only start terminal with script name and it then terminal will execute command from this script. – furas May 17 '21 at 19:26
  • What I can suggest you is, first, use `&` to run in background in the start server cmd. Second, redirect the stdout to a specific file for each server using `&>` – Saurav Kumar May 17 '21 at 19:33

0 Answers0