0

I have scenario like below :

I have a bash file named infra.sh. Inside this infra.sh file I have the below commands:

#!/bin/bash
gnome-terminal -e 'sh -c "bash ./redis-server.sh && sleep 30"'    
gnome-terminal -e 'sh -c "bash ./redis-client.sh && sleep 30"'    
gnome-terminal -e 'sh -c "bash ./playServer.sh && sleep 30"'    
gnome-terminal -e 'sh -c "bash ./proclient-service.sh && sleep 30"'    
gnome-terminal -e 'sh -c "bash ./infoServer.sh"'

Now if I execute the infra.sh from a terminal by typing

sudo sh ./infra.sh

then, all the commands above gets executed in separate terminals. They are running.

But I want to make it in such a way that I will start the infra.sh in a manner that all the services will be running in background and as well as the terminal from which I executed the commands for starting infra.sh will also go in background.

Is it possible?

thanks in advance.

George Udosen
  • 35,970
  • 13
  • 99
  • 121
  • 2
    So the goal is to start services in background. But why use `gnome-terminal` ? If your goal is to preserve output, then consider using `screen` instead. It's often used to start sessions on remote servers, where you can attach to session and detach whenever necessary and don't have to have terminal constantly open. See https://askubuntu.com/q/62562/295286 for details – Sergiy Kolodyazhnyy Dec 24 '18 at 10:54
  • hey @Sergiy Kolodyazhnyy Yes you are right. Currently I am just watching the req and response printed on the server console. so I am running them in terminal. But I want them not to show in terminal now and instead run those services absolutely in background. what should I do? I do not want all of my service terminal constantly open – Biplab Bhattacharya Dec 24 '18 at 10:59
  • There's several options depending on your needs. If you don't care about the output at all, you can use `setsid` command. I'd make all commands be like `setsid ./redis-server.sh 2>&1 > /dev/null` and separate each on separate line with `sleep 30`. See [related post] (https://askubuntu.com/a/106359/295286). If you do still care about preserving output, `screen` which i mentioned already would be more appropriate. You could also consider making the script into a systemd service, but if there's no need to run it at boot, I wouldn't bother. I'd recommend `setsid` first – Sergiy Kolodyazhnyy Dec 24 '18 at 11:06

1 Answers1

2

Two options here:

  1. gnome-terminal <command> &: will run that command in the background but will be ended if the terminal is closed.

  2. nohup gnome-terminal <command> &: will run in background and keep running even if the terminal window is closed.

Now run that script with either & or nohup like so nohup sudo sh ./infra.sh & or sudo sh ./infra.sh &

George Udosen
  • 35,970
  • 13
  • 99
  • 121