3

I wrote this script to create multiple tabs in the same terminal window and run commands in them:

#!/bin/bash
#Use the commands as:
#--tab-with-profile=Default --title=<SPECIFY THE TAB TITLE HERE> -e "bash -ic \"<SPECIFY THE COMMAND YOU WANT TO RUN HERE>; bash\"" \
#The ampersand in the end of this file makes sure that the gnome-terminal command is run as a background process

echo "Setting up simulator environment";
service mysqld start;

gnome-terminal \
  --tab-with-profile=Default --title=PROG1 -e "bash -ic \"clear;ls;./prog1; bash disown\"" \
  --tab-with-profile=Default --title=SIMULATOR -e "bash -ic \"clear;ls;./simulator; bash disown\"" \
  --tab-with-profile=Default --title=PROG2 -e "bash -ic \"clear;ls;./prog2; bash disown\"" \
  --tab-with-profile=Default --title=DATA -e "bash -ic \"./data -f \"/home/user/NetBeansProjects/data3.txt\" -p \"6785\"; bash disown\"" \
  --tab-with-profile=Default --title=PROG3 -e "bash -ic \"cd /home/user/NetBeansProjects/simulator;./prog3; bash disown\"" \
&

Problem is, when any of those programs finish running or if I press Ctrl+c to stop any of those programs, the tab closes. I don't want the tab to close. I want the tab to remain open and the bash terminal to be shown so that I can run some other command in the tab. Is there a way to make that happen?

4

There are two problems;

The first one is that inside each bash -ic command (which by the way doesn't spawn an interactive shell because -c overrides -i, so -i it's safe to be dropped) you're calling bash disown instead of bash, which means nothing and immediately exits on error; so there's no interactive shell keep running that keeps gnome-terminal opened at the end of the outer bash -c command;

(Also mind that you could use exec bash instead of bash at the end of the command, to save some processes.)

The second one is that Ctrl+C SIGINTs all the processess in the same group of the killed process, including the parent bash instance which is supposed to spawn the interactive shell at the end of the command;

To fix this, you can use bash's trap built-in to set bash to spawn another interactive bash instance upon the reception of a SIGINT signal.

In short, this should work:

gnome-terminal \
  --tab-with-profile=Default --title=PROG1 -e "bash -c \"trap 'bash' 2; clear;ls;./prog1; exec bash\"" \
  --tab-with-profile=Default --title=SIMULATOR -e "bash -c \"trap 'bash' 2; clear;ls;./simulator; exec bash\"" \
  --tab-with-profile=Default --title=PROG2 -e "bash -c \"trap 'bash' 2; clear;ls;./prog2; exec bash\"" \
  --tab-with-profile=Default --title=DATA -e "bash -c \"trap 'bash' 2; ./data -f \"/home/user/NetBeansProjects/data3.txt\" -p \"6785\"; exec bash\"" \
  --tab-with-profile=Default --title=PROG3 -e "bash -c \"trap 'bash' 2; cd /home/user/NetBeansProjects/simulator;./prog3; exec bash\"" \
&
  • Awesome! The new bash terminal causes the assigned title of the tab to disappear, so a solution to that would make the script perfect. I tried bash --tab -t "X" but it didn't work. – Nav Oct 7 '15 at 3:11
  • @Nav Have a look here; personally I'd avoid the first solution; the exact same thing can be done using the second solution or the third solution; the second one is a bit "invasive", as it will affect any gnome-terminal instance and any XTerm compatible terminal; personally I would go for the third option and setup a custom profile. – kos Oct 7 '15 at 3:25
  • Works! Had to go to the Edit menu of the terminal, Profiles > Default. And for the option of When terminal commands set their own titles, I selected Keep initial title. Thank you very much @kos. – Nav Oct 7 '15 at 7:07
  • @Nav You're welcome, glad that it helped. – kos Oct 7 '15 at 7:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.