6

I am running Ubuntu 21.04 and I want to create a script, that would do the following:

  1. Run:

    cd && cd path/to/repo && git pull && npm i && code . && npm run dev
    
  2. Then open a new GNOME Terminal tab using:

    gnome-terminal --tab
    
  3. Then switch to this tab and run:

    cd && cd path/to/repo && git reset --hard && git pull -f && npm i && npm run dev
    

So basically I want one terminal with 2 tabs running 2 development servers.

I tried with xdotool and xte, but it never worked (keys aren't even pressed).

Is it even possible to do what I want?

As suggested below I tried, it gave me failed to run cd so I googled and came up with the following:

gnome-terminal --tab -- /bin/bash -e -c "cd path/to/repo && ls && git pull && npm i && code . && npm run dev" --tab -- /bin/bash -e -c "cd path/to/repo && git reset --hard && git pull -f && npm i && npm run dev"

But the problem is that npm run dev is not supposed to ever stop, and so the second terminal tab is not ever opened. And I need to run them together...

3
  • 1
    What did you try thus far? Running two sequences of commands from the command line of gnome-terminal may not anymore be possible, because any commands need to come at the end nowadays after --. You may need another terminal emulator to be able to automatically set up two tabbed sessions.
    – vanadium
    Oct 11, 2021 at 9:02
  • 3
    Minor note: you don't need cd && cd path/to/repo. Either the path/to/repo is a full path so you just do cd path/to/repo or, if it is relative to your $HOME, you can do cd ~/path/to/repo.
    – terdon
    Oct 11, 2021 at 9:24
  • yeah I know about cd && cd path/to/repo, I am currently copying and pasting the command, so it's sort of my way of git pull && npm i && npm run dev, regardless the current directory.
    – Mike K.
    Oct 11, 2021 at 9:50

2 Answers 2

6

Although gnome-terminal indicates that the -e option is deprecated, it still works in 3.40.3.

# Option “-e” is deprecated and might be removed in a later version of gnome-terminal.
# Use “-- ” to terminate the options and put the command line to execute after it.

Developers seem to forget that this is the only way to do what you want, i.e., automatically set up different working environments in tabs. The "new" syntax, that adds commands to run at the end of the options after -- does not allow to do that. Once the -e option is removed, you will need to move to another terminal emulator for something like this.

So this, for example, still works thus far:

gnome-terminal --tab -e "htop" --tab -e "top"

Replace commands htop and top by your custom scripts.

1
  • Seems about to work, but it fires an error: # Error: Failed to execute child process “cd”: Failed to execve: No such file or directory # Error: Failed to execute child process “cd”: Failed to execve: No such file or directory but it does work with gnome-terminal --tab -e "ping goo.gl" why can't it run cd in there?...
    – Mike K.
    Oct 11, 2021 at 9:55
2

so the content of sh script is:

gnome-terminal --tab --title='frontend' -e "bash -c 'cd path/to/repo && git pull && npm i && code . && npm run dev'";
gnome-terminal --tab --title='backend' -e "bash -c 'cd path/to/repo && git reset --hard && git pull -f && npm i && npm run dev'"

Thanks @vanadium for helping to find out the direction of googling ;)

it does work as intended. If I open a terminal and write sh myScript.sh it'll open 2 extra tabs with backend and frontend repos running.

A little problem is that if I execute the sh file by double clicking on it it will open 2 terminal windows (ugly, I want tabs).

Don't really know how to fix this.

1
  • thanks i was missing the ; at the end of the line
    – arrmani88
    17 hours ago

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .